How to split a string into elements of a string array in C#?

karthikeya Boyini
Published on 24-Aug-2019 16:44:05
Set the string you want to split. string str = "Hello World!"; Use the split() method to split the string into separate elements. string[] res = str.Split(' '); The following is the complete code to split a string into elements of a string array in C#. ... Read More

How do you access jagged arrays in C#?

Ankith Reddy
Published on 24-Aug-2019 16:44:05
A Jagged array is an array of arrays. You can declare a jagged array named scores of type int as. int [][] points; Let us now see how to initialize it. int[][] points = new int[][]{new int[]{10, 5}, new int[]{30, 40}, new int[]{70, 80}, new int[]{ 60, ... Read More

How to sort a list of dictionaries by values of dictionaries in C#?

Samual Sam
Published on 24-Aug-2019 16:44:05
Set the list of dictionaries with keys and values. var d = new Dictionary<string, int>(); d.Add("Zack", 0); d.Add("Akon", 3); d.Add("Jack", 2); d.Add("Tom", 1); Get and sort the keys. var val = d.Keys.ToList(); val.Sort(); You can try to run the following code to sort a list of ... Read More

How to select a random element from a C# list?

Arjun Thakur
Published on 24-Aug-2019 16:44:05
Firstly, set a list in C#. var list = new List<string>{ "one", "two", "three", "four"}; Now get the count of the elements and display randomly. int index = random.Next(list.Count); Console.WriteLine(list[index]); To select a random element from a list in C#, try to run the following code ... Read More

How to use XmlSerializer in C#?

karthikeya Boyini
Published on 24-Aug-2019 16:44:05
Serialization/ De-serialization allow communication with another application by sending and receiving data. With XmlSerializer, you can control how objects are encoded into XML. To perform XML Serialization, you need the following two classes − StreamWriter class XmlSerializer class Call the Serialize method with the parameters of the StreamWriter ... Read More

How to use WriteLine() method of Console class in C#?

Chandu yadav
Published on 24-Aug-2019 16:44:05
WriteLine() is a method of the Console class defined in the System namespace This statement causes the message "Welcome!" to be displayed on the screen as shown below − Example Live Demo using System; namespace Demo { class Test { ... Read More

How to use Try/catch blocks in C#?

Samual Sam
Published on 24-Aug-2019 16:44:05
Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: try, catch, finally, and throw. try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one ... Read More

How to use the ToString() method of array in C#?

George John
Published on 24-Aug-2019 16:44:05
The ToString() method returns a string that represents the current object. In the below example, we have used the ToString() method with another Array class method. arr.GetLowerBound(0).ToString() Example Live Demo using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lower { class Program ... Read More

How to use the Compare method of the string class in C#?

karthikeya Boyini
Published on 24-Aug-2019 16:44:05
The Compare method compares two specified string objects and returns an integer that indicates their relative position in the sort order. Firstly, set the strings. string str1 = "Jack"; string str2 = "Mac"; Now compare the strings using the Compare() method and if the comparison results 0, then ... Read More

How to use the Sort() method of array class in C#?

Ankith Reddy
Published on 24-Aug-2019 16:44:05
The Sort() method sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array. Set the array. int[] list = { 22, 12, 65, 9}; Use the Sort() method to sort the array. Array.Sort(list); The following is an example ... Read More
Advertisements