Found 34494 Articles for Programming

What is boxing in C#?

George John
Updated on 21-Jun-2020 15:05:29

423 Views

Boxing convert value type to an object type. Let us see an example of boxing −int x = 50; object ob = x; // boxingIn boxing, the value stored on the stack is copied to the object stored on heap memory, whereas unboxing is the opposite.Boxing is useful in storing value types in the garbage-collected heap. It is implicit conversion of a value type to the type object.Let us see an example −Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    static void Main() {       int x = 50;       object ... Read More

How to copy or clone a C# list?

Samual Sam
Updated on 21-Jun-2020 15:06:41

2K+ Views

To copy or clone a C# list, firstly set a list −List < string > list1 = new List < string > (); list1.Add("One"); list1.Add("Two"); list1.Add("Three"); list1.Add("Four");Now declare a string array and use the CopyTo() method to copy.string[] arr = new string[20]; list1.CopyTo(arr);Let us see the complete code to copy a list into a one-dimensional array.Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new List < string > ();       list1.Add("One");       list1.Add("Two");       list1.Add("Three");   ... Read More

Generics in C#

Chandu yadav
Updated on 21-Jun-2020 14:43:04

297 Views

Generics allow you to write a class or method that can work with any data type.Write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type. Generics is a technique that enriches your programs in the following ways −It helps you to maximize code reuse, type safety, and performance.You can create generic collection classes. The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. You may use these generic ... Read More

An array of streams in C#

karthikeya Boyini
Updated on 21-Jun-2020 14:44:13

523 Views

Set the string array for the values −string[] names = new string[] {"Jack", "Tom"};Now using foreach array, write the content in the file −using (StreamWriter sw = new StreamWriter("names.txt")) {    foreach (string s in names) {       sw.WriteLine(s);    } }The following is an example showing an array of streams to write text to a file −Exampleusing System; using System.IO; namespace FileApplication {    class Program {       static void Main(string[] args) {          string[] names = new string[] {"Jack", "Tom"};          using (StreamWriter sw = ... Read More

How to append a second list to an existing list in C#?

Arjun Thakur
Updated on 04-Oct-2023 21:27:32

28K+ Views

Use the AddRange() method to append a second list to an existing list.Here is list one −List < string > list1 = new List < string > (); list1.Add("One"); list1.Add("Two");Here is list two −List < string > list2 = new List < string > (); list2.Add("Three"); ist2.Add("Four");Now let us append −list1.AddRange(list2);Let us see the complete code.Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new List < string > ();       list1.Add("One");       list1.Add("Two");     ... Read More

How to add read-only property in C#?

Samual Sam
Updated on 21-Jun-2020 14:47:38

188 Views

A field marked "read-only", can only be set once during the construction of an object. It cannot be changed −Let us see an example.class Employee {    readonly int salary;    Employee(int salary) {       this.salary = salary;    }    void UpdateSalary() {       //salary = 50000; // Compile error    } }Above, we have set the salary field as read-only.If you will change it, then a compile-time error will occur. The same is shown in the above example.Let us now see how to check whether an array is read-only or not −Exampleusing ... Read More

How to add string values to a C# list?

karthikeya Boyini
Updated on 21-Jun-2020 14:51:06

14K+ Views

To add string values to a list in C#, use the Add() method.Firstly, declare a string list in C# −List list1 = new List();Now add string items −myList.Add("Jack"); myList.Add("Ben"); myList.Add("Eon"); myList.Add("Tim");Let us see the complete code −Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo {    public class Program {       public static void Main(String[] args) {          List myList = new List();          myList.Add("Jack");          myList.Add("Ben");          myList.Add("Eon");          myList.Add("Tim");          Console.WriteLine(myList.Count);       }    } }

How to add items/elements to an existing jagged array in C#?

Ankith Reddy
Updated on 21-Jun-2020 14:50:24

586 Views

To add an element to existing jagged array, just set the value of the element with a new value.Let’s say you need to add an element at the following location −a[3][1]Just set the value −a[3][1] = 500;Above, we accessed the first element of the 3rd array in a jagged array.Let us see the complete code −Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          int[][] x = new int[][]{new int[]{10, 20}, new int[]{30, 40}, new int[]{50, 60}, new int[]{ 70, 80 }, new int[]{ 90, 100 ... Read More

How to add integer values to a C# list?

Samual Sam
Updated on 21-Jun-2020 14:58:47

7K+ Views

To add integer values to a list in C#, use the Add() method.Firstly, declare an integer list in C# −List list1 = new List();Now add integer values −list1.Add(900); list1.Add(400); list1.Add(300);Let us see the complete code −Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo {    public class Program {       public static void Main(String[] args) {          List list1 = new List();          list1.Add(900);          list1.Add(400);          list1.Add(300);          Console.WriteLine(list1.Count);       }    } }

How to add items to a list in C#?

Arjun Thakur
Updated on 21-Jun-2020 14:58:17

676 Views

Firstly, declare a list −var teams = new List();To add items to a C# list, use the Add() method −teams.Add("US"); teams.Add("Canada"); teams.Add("India"); teams.Add("Australia");You can try to run the following code to add items to a list in C# −Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       var teams = new List();       teams.Add("US");       teams.Add("Canada");       teams.Add("India");       teams.Add("Australia");           Console.WriteLine("Elements...");       foreach (var countries in teams) {          Console.WriteLine(countries);       }    } }

Advertisements