- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2628 Articles for Csharp
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
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
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
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
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); } } }
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
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); } } }
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); } } }
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