Server Side Programming Articles - Page 2410 of 2646

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

How do you declare, initialize and access jagged arrays in C#?

Arjun Thakur
Updated on 23-Jun-2020 14:18:11

244 Views

Declare Jagged ArrayA Jagged array is an array of arrays. You can declare a jagged array named scores of type int as −int [][] points;Initialize Jagged ArrayLet 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 ElementAccess the jagged array element as.points[i][j]);The following is the complete example showing how to work with jagged arrays in C#.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          int[][] points = new int[][]{new ... Read More

How to sort one-dimensional array in ascending order using non-static method?

Chandu yadav
Updated on 23-Jun-2020 14:20:04

2K+ Views

Set the unsorted array first.int[] list = {87, 45, 56, 22, 84, 65};Now use a nested for loop to sort the list, which is passed to a function.for(int i=0; i< arr.Length; i++) {    for(int j=i+1; j=arr[j]) {          temp=arr[j];          arr[j]=arr[i];          arr[i]=temp;       }    }    Console.Write(arr[i] + " "); }The following is the complete code to sort one-dimensional array in ascending order using non-static method.Example Live Demousing System; namespace Demo {    public class MyApplication {       public static void Main(string[] args) {   ... Read More

How to use the return statement in C#?

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

625 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

821 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 IndexOf(,) method of array class in C#?

karthikeya Boyini
Updated on 23-Jun-2020 12:41:47

14K+ Views

The IndexOf() method of array class in C# searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.We have set the array.int[] arr = new int[10]; arr[0] = 100; arr[1] = 200; arr[2] = 300; arr[3] = 400; arr[4] = 500; arr[5] = 600; arr[6] = 700; arr[7] = 800; arr[8] = 900; arr[9] = 1000;Now use the IndexOf() method and set the element for which you want the index, for example, I have set it for element 800.int a = Array.IndexOf(arr, 800);The following is the example showing the usage of IndexOf(, ... 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

How to sort one dimensional array in descending order using non static method?

Arjun Thakur
Updated on 23-Jun-2020 12:44:58

248 Views

Set the unsorted list first.int[] list = {87, 45, 56, 22, 84, 65};Now use a nested for loop to sort the list that is passed to a function.for(int i=0; ilt; arr.Length; i++) {    for(int j=i+1; j

How to use the GetType method of array class in C#?

karthikeya Boyini
Updated on 23-Jun-2020 12:45:29

3K+ Views

The GetType() method of array class in C# gets the Type of the current instance (Inherited from Object).To get the type.Type tp = value.GetType();In the below example, we are checking the int value using the type.if (tp.Equals(typeof(int))) Console.WriteLine("{0} is an integer data type.", value)The following is the usage of GetType() method in C#.Exampleusing System public class Program {    public static void Main() {       object[] values = { (int) 100, (long) 17111};       foreach (var value in values) {          Type tp = value.GetType();          if (tp.Equals(typeof(int)))     ... Read More

Advertisements