Found 33676 Articles for Programming

Main thread vs child thread in C#

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

877 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

722 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

381 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

212 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

509 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

4K+ 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

314 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

Deadlock and Starvation in C#

George John
Updated on 22-Jun-2020 10:52:20

1K+ Views

Deadlock occurs when a resource is locked by a thread and is required by another thread at the same time. This problem occur frequenty in a multiprocessing system.It can occur when two or more threads wait for a resource that belon to another thread. Here is an example −Thread OneThread TwoTakes Lock PTakes Lock QRequests Lock QRequests Lock PThread One will not get Lock Q since it belongs to Thread Two. In the same way, Thread Two won’t get Lock P since its original owner is Thread One.Deadlocks can also be a three-way deadlock that occurs if three threads and ... Read More

C# program to merge two Dictionaries

Chandu yadav
Updated on 22-Jun-2020 10:52:52

2K+ Views

Set the two dictionaries −Dictionary < string, int > dict1 = new Dictionary < string, int > (); dict1.Add("laptop", 1); dict1.Add("desktop", 2); Dictionary < string, int > dict2 = new Dictionary < string, int > (); dict2.Add("desktop", 3); dict2.Add("tablet", 4); dict2.Add("mobile", 5);Now use HashSet and UnionWith() method to merge the two dictionaries −HashSet < string > hSet = new HashSet < string > (dict1.Keys); hSet.UnionWith(dict2.Keys);Here is the complete code −Exampleusing System; using System.Collections.Generic; class Program {    static void Main() {       Dictionary < string, int > dict1 = new Dictionary < string, int > ();     ... Read More

How to find the file using C#?

George John
Updated on 22-Jun-2020 10:53:53

149 Views

Use the GetDirectories in C# to get a list of sub-folder that appears first −Directory.GetDirectoriesNow loop through those directories and repeat the process for the sub folder.string path = @"d:/New/Myfile"; string[] myDir = Directory.GetDirectories(path, "xml", SearchOption.AllDirectories); Console.WriteLine(myDir.Length.ToString()); foreach (string res in myDir) Console.WriteLine(res);

Advertisements