Found 34494 Articles for Programming

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

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

305 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 Compare method of the string class in C#?

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

176 Views

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 it would mean the strings are equal.String.Compare(str1, str2) == 0Let us see the complete example.Example Live Demousing System; namespace StringApplication {    class StringProg {       static void Main(string[] args) {          string str1 = "Jack";          string str2 = "Mac";          if (String.Compare(str1, str2) ... Read More

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

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

163 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 to use the SetValue(,) method of array class in C#?

Samual Sam
Updated on 23-Jun-2020 14:17:33

1K+ Views

The SetValue() method sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.Firstly, set the array.Array arr = Array.CreateInstance(typeof(String), 6);No set values to the element using the SetValue() method.arr.SetValue("One", 0); arr.SetValue("Two", 1); arr.SetValue("Three", 3); arr.SetValue("Four", 4); arr.SetValue("Five", 5);The following is an example showing the usage of SetValue() method in C#.Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class Program {       static void Main(string[] args) {          Array arr = Array.CreateInstance(typeof(String), 6);          arr.SetValue("One", 0); ... Read More

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

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

121 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 use the Reverse() method of array class in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:18:57

7K+ Views

The Reverse() method in array class reverses the sequence of the elements in the entire one-dimensional Array.To reverse an array, just use the Array.Reverse() method −Array.Reverse(temp);Within the reverse method, set the elements like the following code snippet.int[] list = { 29, 15, 30, 98}; int[] temp = list;You can try to run the following code to implement Reverse() method in C#.Example Live Demousing System; namespace Demo {    class MyArray {       static void Main(string[] args) {          int[] list = { 29, 15, 30, 98};          int[] temp = list;     ... 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

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

499 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

615 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

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

Advertisements