SiteMap  | About    

C# Array Examples

Arrays are using for store similar data types grouping as a single unit. We can access Array elements by its numeric index. The array indexes start at zero. The default value of numeric array elements are set to zero, and reference elements are set to null .

c# array
Integer array c#

Declaring and Initializing an Integer Array

int[] array = new int[4];

array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;

In the above code we declare an Integer Array of four elements and assign the value to array index . That means we assign values to array index 0 - 4.

We can retrieve these values from array by using a for loop.

for (int i = 0; i < array.Length; i++) { MessageBox.Show (array[i]); }

Initialize array c#

Also we can declare and initialize an array in one statement.

int[] array = new int[] {10, 20, 30, 40};

Note that in the above code we did not specify the length of the array so the compiler will do it for us.

How to find the length of an Array ?


We can use array.Length to find the length of an Array.

String array c#

Declaring and Initializing a String Array

string[] week = new string[7]; week[0] = "Sunday"; week[1] = "Monday";

The above C# code declare a string array of 7 strings and assign some values to it.

string[] week = new string[] {"Sunday","Monday","Tuesday"};

The above code declare and initialize a string array with values.

string str = week[1];

We can access the Arrays elements by providing its numerical index, the above statement we access the second value from the week Array.

In the following program , we declare an Array "week" capable of seven String values and assigns the seven values as days in a week . Next step is to retrieve the elements of the Array using a for loop . For finding the end of an Array we used the Length function of Array Object.

string[] week = new string[7]; week[0] = "Sunday"; week[1] = "Monday"; week[2] = "Tuesday"; week[3] = "Wednsday"; week[4] = "Thursday"; week[5] = "friday"; week[6] = "Saturday"; for (int i = 0; i &lt; = week.Length-1; i++) { MessageBox.Show(week[i]); }

resize array c#

An array can be resized with Array.Resize < T > Method , that means We make an array bigger or smaller. Array.Resize < T > Method Changes the number of elements of a one-dimensional array to the specified new size.

Array.Resize < T > - T is the type of the elements of the array.

This method should be used with only one dimensional Array. This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.

Resize Array

// Initialize array for example char[] array = new char[5]; array[0] = 'A'; array[1] = 'B'; array[2] = 'C'; array[3] = 'D'; array[4] = 'E'; for (int i = 0; i < array.Length; i++) { MessageBox.Show (array[i].ToString ()); } Array.Resize(ref array, 3); for (int i = 0; i < array.Length; i++) { MessageBox.Show(array[i].ToString ()); }

Array.Resize(ref array, 3);

In the above code we resize the array to 3 elements.

for loop array c#
int[] array = { 10, 30, 50 }; //array declaration foreach (int element in array) { Console.WriteLine(element); }

Array Sort

sort array in c# ascending descending Reverse array

You can sort the arrays in ascending order as well as descending . We can use Array.Sort method for sorts the elements in a one-dimensional array. Also we can use Array.Reverse method for reverses the sequence of the elements in the entire one-dimensional Array. Click the following link to see .. How to sort C# Arrays

How to check if a value exists in an array ?

The following program shows how to find an element from an Array

string stringToCheck = "GHI";
string[] stringArray = { "ABC", "DEF", "GHI", "JKL" };
foreach (string x in stringArray)
    if (x.Equals (stringToCheck))
        MessageBox.Show("Find the string ..." + x);

 CSharp Collection - Related Contents

 CSharp Collection - Related Programs