Found 34494 Articles for Programming

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

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

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

667 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

960 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

371 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 do you access jagged arrays in C#?

Ankith Reddy
Updated on 23-Jun-2020 14:26:01

93 Views

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, 70 }};Access the jagged array element as −int x = points[0][1];The following is the complete example showing how to access jagged arrays in C#.Exampleusing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          int[][] points = new int[][]{new int[]{10, 5}, new int[]{30, 40}, new int[]{70, ... Read More

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

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

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

256 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

461 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 Try/catch blocks in C#?

Samual Sam
Updated on 23-Jun-2020 14:14:50

2K+ Views

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 or more catch blocks.catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.The following is an example showing how to use the try, catch, and finally in C#.Example Live Demousing ... Read More

Advertisements