Found 2628 Articles for Csharp

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

187 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

584 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

675 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);       }    } }

Find all substrings in a string using C#

karthikeya Boyini
Updated on 21-Jun-2020 14:25:18

356 Views

Use the substring() method in C# to find all substrings in a string.Let’s say our string is −pqrLoop through the length of the string and use the Substring function from the beginning to the end of the string −for (int start = 0; start

File Handling in C#

George John
Updated on 21-Jun-2020 14:34:14

571 Views

A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.In C#, you need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows −FileStream = new FileStream( , , , );Here, the file operations are also included as shown below −The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are −Append − It opens an existing ... Read More

Advertisements