Found 34494 Articles for Programming

How to initialize a list to an empty list in C#?

karthikeya Boyini
Updated on 06-Sep-2023 20:56:09

42K+ Views

To initialize a list to an empty list in C#, set it like the following statement without any elements −List list = new List();Now, use the Any() method to check whether the list is empty or not −bool chk = !list.Any();Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Program {    public static void Main() {       // empty list       List list = new List();       // check for empty list       bool chk = !list.Any();       if(chk) {          Console.WriteLine("List is Empty!");       } else {          Console.WriteLine("List isn't Empty!");       }    } }OutputList is Empty!

How to initialize a dictionary to an empty dictionary in C#?

Samual Sam
Updated on 22-Jun-2020 11:59:00

11K+ Views

To initialize a dictionary to an empty dictionary, use the Clear() method. It clears the dictionary and forms it as empty.dict.Clear();After that, use the Dictionary count property to check whether the list is empty or not −if (dict.Count == 0) {    Console.WriteLine("Dictionary is empty!"); }Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; namespace Demo {    public class Program {       public static void Main(string[] args) {          var dict = new Dictionary < string, string > ();          dict.Clear();          if (dict.Count == 0) {             Console.WriteLine("Dictionary is empty!");          }       }    } }OutputDictionary is empty!

Sets in C#

mkotla
Updated on 22-Jun-2020 11:57:09

6K+ Views

Sets in C# is a HashSet. HashSet in C# eliminates duplicate strings or elements in an array. In C#, it is an optimized set collectionTo declare HashSet −var h = new HashSet(arr1);Above, we have set the already declared array arr1 in the HashSet.Now set it on the array to remove the duplicate words −string[] arr2 = h.ToArray();Let us see an example to remove duplicate strings using C# HashSet.Here, we have duplicate elements −Exampleusing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       string[] arr1 = {"Table", "Chair", "Pen", "Clip", "Table"};     ... Read More

Main thread vs child thread in C#

varma
Updated on 22-Jun-2020 12:00:04

628 Views

Main ThreadThe first thread to be executed in a process is called the main thread. When a C# program starts execution, the main thread is automatically created.Child ThreadThe threads created using the Thread class are called the child threads of the main thread.Here is an example showing how to create a main and child thread −Example Live Demousing System; using System.Threading; namespace Demo {    class Program {       static void Main(string[] args) {          Thread th = Thread.CurrentThread;          th.Name = "MainThread";          Console.WriteLine("This is {0}", th.Name);          Console.ReadKey();       }    } }OutputThis is MainThread

Valid variant of Main() in C#

varun
Updated on 22-Jun-2020 12:01:04

420 Views

The Main method is the entry point for all C# programs. It states what the class does when executed.The valid variant of Main() is −static void Main(string[] argsHere,static − the object is not needed to access static membersvoid − return type of the methodMain − entry point for any C# program. Program execution begins here.string[] args − for command line arguments in C#.ExampleHere is an example −using System; namespace Program {    public class Demo {       public static void Main(string[] args) {          Console.WriteLine("Welcome!");          Console.ReadKey();       }    }   }

How to convert a string into int in C#?

George John
Updated on 22-Jun-2020 11:00:34

245 Views

Let’s say our string is −string str ="9999";Now, use the Int32.Parse() to convert the string into integer −int n = Int32.Parse(str);Now display the integer value as shown in the following code −Exampleusing System; class Demo {    static void Main() {       string str ="9999";       int n = Int32.Parse(str);       Console.WriteLine(n);    } }

How to convert an array of characters into a string in C#?

Arjun Thakur
Updated on 22-Jun-2020 11:01:48

135 Views

Let us first set an array of 5 characters −char[] ch = new char[15]; ch[0] = 'T'; ch[1] = 'r'; ch[2] = 'i'; ch[3] = 'c'; ch[4] = 'k';Now convert them into a string −string str = new string(ch);Here is the complete code −ExampleUsing System; class Program {    static void Main() {       char[] ch = new char[15];       ch[0] = 'T';       ch[1] = 'r';       ch[2] = 'i';       ch[3] = 'c';       ch[4] = 'k';       // converting to string       string str = new string(ch);       Console.WriteLine(str);    } }

How to clone a generic list in C#?

Chandu yadav
Updated on 22-Jun-2020 11:02:47

355 Views

A list is a Generic collection to hold elements of same datatypes.To clone a list, you can use the CopyTo method.Declare a list and add elements −List < string > myList = new List < string > (); myList.Add("Programming"); myList.Add("Web Dev"); myList.Add("Database");Now create a new array and clone the list into it −string[] arr = new string[10]; myList.CopyTo(arr);Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       List < string > myList = new List < string > ();       myList.Add("Programming");       ... Read More

Semaphore in C#

Arjun Thakur
Updated on 01-Apr-2020 08:47:56

3K+ Views

The semaphore class lets you set a limit on the number of threads that have access to a critical section. The class is used to control access to a pool of resources. System.Threading.Semaphore is the namespace for Semaphore because it has all the methods and properties required to implement Semaphore.For using a semaphore in C#, you just need to instantiate an instance of a Semaphore object. It has minimum of two arguments −Reference−MSDNSr.No.Constructor & Description1Semaphore(Int32, Int32)Initializes a new instance of the Semaphore class, specifying the initial number of entries and the maximum number of concurrent entries.2Semaphore(Int32, Int32, String) −Initializes a ... Read More

Synchronization of ArrayList in C#

Ankith Reddy
Updated on 22-Jun-2020 10:52:04

209 Views

Use the ArrayList.Synchronized Method in C# for synchronization of ArrayList in C#.Let us see an example to lock the collection using SyncRoot property in C# −ArrayList arr = new ArrayList(); lock(arr.SyncRoot) {    foreach (object ele in arr) {    } }The following is the complete example to check the synchronization status of ArrayList −Example Live Demousing System; using System.Collections; public class Demo {    public static void Main() {       ArrayList arr1 = new ArrayList();       arr1.Add("One");       arr1.Add("Two");       arr1.Add("Three");       arr1.Add("Four");       arr1.Add("Five");     ... Read More

Advertisements