Found 34488 Articles for Programming

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

136 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

357 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

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

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

89 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);

LinkedList in C#

Samual Sam
Updated on 22-Jun-2020 10:55:35

2K+ Views

System.Collections.Generic namespace is available in C# for LinkedList. The LinkedList class allows insertion and deletion of elements from the list at a fast pace.C# LinkedList class uses the concept of linked list. It allows us to insert and delete elements fastly. It can have duplicate elements. It is found in System.Collections.Generic namespace.Here is an example −Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       LinkedList < string > l = new LinkedList < string > ();       l.AddLast("one");       l.AddLast("two");       l.AddLast("three");     ... Read More

How to write "Hello World" in C#?

karthikeya Boyini
Updated on 22-Jun-2020 10:56:46

400 Views

To print “Hello World” in C#, use the Console.WriteLine.Let us see a basic C# program to display a text −Example Live Demousing System; using System.Collections.Generic; using System.Text; namespace Program {    class MyApplication {       static void Main(string[] args) {          Console.WriteLine("Hello World");          Console.Read();       }    } }OutputHello WorldAbove, we displayed the text “Hello World” using the WriteLine() method. The output is displayed using the Console −Console.WriteLine("Hello World");

Advertisements