Working with Array in C#

In this post I will explain how to work with Array in C#

Array in C# is a collection of objects of same data type. An Array is declared by appending square brackets with the data type. Array supports zero based indexing. First element of an array is stored at 0 index

Declaring and initializing an Array

An Array is declared by appending square brackets with the data type. For example a string array can be declared as following:

string[] Rainbow;

An Array is a reference type, so it is initialized using new keyword. An Array can be initialized using any of the following syntax. In the first example, array is declared with size 7 and its element is stored using its index starting from 0 to 6. In other two example array is declared and assigned on the same line

string[] Rainbow = new string[7];
Rainbow[0] = "Violet";
Rainbow[1] = "Indigo";
Rainbow[2] = "Blue";
Rainbow[3] = "Green";
Rainbow[5] = "Yellow";
Rainbow[4] = "Orange";
Rainbow[6] = "Red";
string[] Rainbow = new string[7] {"Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red" };
string[] Rainbow = { "Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red" };

Accessing an Array

Array elements can be accessed using its indexes. First element is stored at index position 0, so in the above array Rainbow[0] will return Violet and so on. To access all the elements of an array we can use following code

foreach (string color in Rainbow)
{
     Console.WriteLine(color);
}

Or

for (int i = 0; i < Rainbow.Length; i++)
{
     Console.WriteLine(Rainbow[i]);
}

Length property is used to get total number of elements in the array.

Sorting an Array

An array can be sorted using Array.Sort() method

Output of array before sorting:

     Violet
     Indigo
     Blue
     Green
     Yellow
     Orange
     Red
Array.Sort(Rainbow);

Output of array after sorting:

     Blue
     Green
     Indigo
     Orange
     Red
     Violet
     Yellow

Array.Sort method have many overloaded forms that we can use for conditional sorting of an array.

Reversing an Array

An array can be reversed using Array.Reverse() method

Array.Reverse(Rainbow);

Output of above array after sorting

     Red
     Orange
     Yellow
     Green
     Blue
     Indigo
     Violet

A few elements of an array can also be reversed using Array.Sort method. Like if you want to reverse 5 elements from third position you can use overloaded Array.Sort method

Array.Reverse(Rainbow, 2, 5);

Output after this :

     Violet
     Indigo
     Red
     Orange
     Yellow
     Green
     Blue

Copying an Array

A copy of an array can be created using Array.Copy() method. For copying an array in a new array, an array of same or more length has to be created. For example, a new array “Colors” has been created with length “7” and Array.Copy() method is used to copy array. It takes three parameters, first is “Source array”, second is “Destination array” and third is number of elements to copy.

string[] Colors = new string[7];
Array.Copy(Rainbow, Colors, 7);

You can also use CopyTo() method of array to copy an array. It takes two parameters, “Destination array” and index position at which copy will start.

Rainbow.CopyTo(Colors, 0);

Clone() is another method of an array that is used to copy arrays. It creates shallow copy of an array. Here we just have to declare an array. We do not need to initialize it.

string[] Colors;
Colors = (string[])Rainbow.Clone();

One thought on “Working with Array in C#

Leave a Reply

Your email address will not be published. Required fields are marked *