Csharp Articles - Page 99 of 196

How do we call a C# method recursively?

Samual Sam
Updated on 23-Jun-2020 14:21:46

343 Views

To call a C# method recursively, you can try to run the following code. Here, Factorial of a number is what we are finding using a recursive function display().If the value is 1, it returns 1 since Factorial is 1.if (n == 1) return 1;If not, then the recursive function will be called for the following iterations if 1you want the value of 5!Interation1: 5 * display(5 - 1); Interation2: 4 * display(4 - 1); Interation3: 3 * display(3 - 1); Interation4: 4 * display(2 - 1);The following is the complete code to call a C# method recursively.Example Live Demousing System; ... Read More

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

911 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 sort a list of dictionaries by values of dictionaries in C#?

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

513 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

Mobile

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 XmlSerializer in C#?

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

614 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

529 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 return statement in C#?

Samual Sam
Updated on 23-Jun-2020 12:40:43

637 Views

The return statement is used to return value. When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.The following is an example to learn about the usage of return statement in C#. Here, we are finding the factorial of a number and returning the result using the return statement.while (n != 1) {    res = res * n;    n = n ... Read More

How to use the Main() method in C#?

George John
Updated on 23-Jun-2020 12:41:09

834 Views

A main method is static since it is available to run when the C# program starts. It is the entry point of the program and runs without even creating an instance of the class.The Main method states what the class does when executed and instantiates other objects and variables.The following shows how to add a Main() method.Exampleusing system; namespace demo {    class helloworld {       static void main(string[] args) {          console.writeline("hello world");          console.readkey();       }    } }As you can see in the above example.static void Main(string[] ... Read More

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

Ankith Reddy
Updated on 23-Jun-2020 12:42:50

3K+ Views

The GetValue() method of array class in C# gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.We have set the array values first using the Array.CreateInstance method.Array arr = Array.CreateInstance(typeof(String), 3, 6); arr.SetValue("One", 0, 0); arr.SetValue("Two", 0, 1); arr.SetValue("Three", 0, 2); arr.SetValue("Four", 0, 3); arr.SetValue("Five", 1, 4); arr.SetValue("Six", 1, 5); arr.SetValue("Seven", 1, 2); arr.SetValue("Eight", 1, 3);Then loop throught the array length. This will display all the values using the GetValue() method.for (int i = 0; i < a; i++) for (int j = 0; j < b; j++) Console.WriteLine( arr.GetValue(i, ... Read More

Advertisements