Csharp Articles - Page 125 of 258

How do we access elements from the two-dimensional array in C#?

Chandu yadav
Updated on 23-Jun-2020 14:23:36

4K+ Views

A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns.An element in 2-dimensional array is accessed by using the subscripts. That is, row index and column index of the array.int x = a[1, 1]; Console.WriteLine(x);Let us see an example that shows how to access elements from two-dimensional array.Example Live Demousing System; namespace Demo {    class MyArray {       static void Main(string[] args) {          /* an array with 5 rows and 2 columns*/          int[, ] a = new int[5, 2] ... Read More

How to split a string with a string delimiter in C#?

Samual Sam
Updated on 23-Jun-2020 14:24:32

890 Views

Delimiters are the commas that you can see in the below string.string str = "Welcome, to, New York";Now set the delimiter separately.char[] newDelimiter = new char[] { ', ' };Use theSplit() method to split the string considering the delimiter as the parameter.str.Split(newDelimiter, StringSplitOptions.None);To split a string with a string deli meter, try to run the following code −Example Live Demousing System; class Program {    static void Main() {       string str = "Welcome, to, New York";       char[] newDelimiter = new char[] { ', ' };       string[] arr = str.Split(newDelimiter, StringSplitOptions.None);     ... Read More

How to split a string using regular expressions in C#?

George John
Updated on 23-Jun-2020 14:24:54

1K+ Views

To split a string suing regular expression, use the Regex.split.Let’s say our string is −string str = "Hello\rWorld";Now use Regex.split to split the string as shown below −tring[] res = Regex.Split(str, "\r");The following is the complete code to split a string using Regular Expression in C#.Example Live Demousing System; using System.Text.RegularExpressions; class Demo {    static void Main() {       string str = "Hello\rWorld";       string[] res = Regex.Split(str, "\r");       foreach (string word in res) {          Console.WriteLine(word);       }    } }OutputHello World

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

karthikeya Boyini
Updated on 23-Jun-2020 14:25:16

1K+ Views

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#.Example Live Demousing System; class Demo {    static void Main() {       string str = "Hello World!";       string[] res = str.Split(' ');       Console.WriteLine("Separate elements:");       foreach (string words in res) {          Console.WriteLine(words);       }    } }OutputSeparate elements: Hello World!

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

Samual Sam
Updated on 23-Jun-2020 14:26:29

500 Views

Set the list of dictionaries with keys and values.var d = new Dictionary(); 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 dictionaries by values.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       var d = new Dictionary();       d.Add("Zack", 0);       d.Add("Akon", 3);       d.Add("Jack", 2);       d.Add("Tom", 1);       // Acquire keys and sort them.       var val ... Read More

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

Arjun Thakur
Updated on 23-Jun-2020 14:12:45

45K+ Views

Firstly, set a list in C#.var list = new List{ "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 −Example Live Demousing System; using System.Collections.Generic; namespace Demo {    class Program {       static void Main(string[] args) {          var random = new Random();          var list = new List{ "one","two","three","four"};          int index = random.Next(list.Count);          Console.WriteLine(list[index]);       }    } }Outputthree

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

Chandu yadav
Updated on 23-Jun-2020 14:13:17

392 Views

WriteLine() is a method of the Console class defined in the System namespaceThis statement causes the message "Welcome!" to be displayed on the screen as shown below −Example Live Demousing System; namespace Demo {    class Test {       static void Main(string[] args) {          Console.WriteLine("Welcome!");          Console.ReadKey();       }    } }OutputWelcome!To display a char array using the Console.WriteLine.Example Live Demousing System; namespace Demo {    class Test {       static void Main(string[] args) {          char[] arr = new char[] { 'W', 'e'};          Console.WriteLine(arr);          Console.ReadKey();       }    } }OutputWe

How to use XmlSerializer in C#?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

594 Views

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 and object to serialize. string myPath = "new.xml"; XmlSerializer s = new XmlSerializer(settings.GetType()); StreamWriter streamWriter = new StreamWriter(myPath); s.Serialize(streamWriter, settings); An XML file is visible with the name “new.xml”. Now to deserialize. MySettings mySettings = new MySettings(); string myPath = "new.xml"; XmlSerializer ... Read More

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

George John
Updated on 23-Jun-2020 14:15:29

513 Views

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 Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lower {    class Program {       static void Main(string[] args) {          Array arr = Array.CreateInstance(typeof(String), 3);          arr.SetValue("One", 0);          arr.SetValue("Two", 1);          Console.WriteLine("Lower Bound {0}",arr.GetLowerBound(0).ToString());          Console.ReadLine();       }    } }OutputLower Bound 0

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

Ankith Reddy
Updated on 23-Jun-2020 14:16:54

282 Views

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 to learn how to work with the Sort() method.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          int[] list = { 22, 12, 65, 9};          Console.Write("Original Array: ");          foreach (int i in list) {         ... Read More

Advertisements