What is an Array in C#?

An array is a collection of similar type variables which is fixed in size. To understand array, consider this example.

If you have to store US state names in variables then you have to create 52 variables like that:

USstate1, USstate2 and so on. Whereas if you use arrays, you can store all states in a single array as follows:

string[] USstates = new string[52];

See an example of array

A few main points about C# array:

  • An array is a collection of similar type variables.
  • The array is fixed size.
  • The arrays are basically objects.
  • The base class of the array in C# is Array which is defined in the System namespace.
  • You can use properties and methods of Array class e.g length, sort, Clear etc. (Examples are given below).
  • Array starts at index 0, which in integer based.
  • The elements of an array can be accessed by using the index number.
  • C# supports single, multi-dimensional and jagged arrays. The jagged arrays are the array of arrays.
  • As such the C sharp array is fixed size, for the dynamic collection you can use the C# list.

How to declare or initialize an array?

This is how you can define an array in C#:

Single dimensional array:

Int[] intarray;

Or

Int[] intarray = new int[10];

Multi-dimensional arrays:

String[,] mul_array;

Where intarray and mul_array are the array names. The array can be created by using the new keyword as shown in the second line.

Now as we learned what are the C# arrays and how to initialize it let us look at a few examples of single and multi-dimensional arrays.

An int array example

Following is an example of an int type array where we will declare an array. After that, we will create array elements which is followed by displaying elements by using a foreach loop. See the example by clicking the link or image below.

Experience this example online

This is how the array example worked:

First of all, we initialized an int type array, intarray, of four elements. Then we assigned values to elements of arrays based on index numbers, starting from 0.

Lastly, we used a foreach loop to go through intarray elements to display array elements.

C# string array example

Following is a string array example. In this example, we will create a string array with 4 US State names. After assigning values to the array elements, we will use the foreach loop to display string array elements.

Experience this example online

You can see the string array elements, that are US states, are displayed in the same order as given by using the foreach loop.

An array example for accessing specific element

You can access the specific element of the array by using index number. As mentioned earlier, the index of arrays C# starts at 0. Following example accesses the third element of string array and displays its value.

Experience this example online

You can see, we used array name and element index in brackets, USStates[2] that displayed Florida.

An array with for loop example

In this example, we will use the for loop to display element of an array. We created a string array with four elements (US State names).

In the for loop, we used the length property of array class to get the total number of elements in the array. See example by clicking the link below:

Experience this example online

Useful array methods

As such, the Array is a class of System namespace. It has properties and methods that you can use to perform useful actions in the arrays. Let us look at a few useful methods of array class with examples.

The array sort method

The array class has sort method that is used to sort the given array. Following example simply takes an array and sorts it alphabetically.

For this example, we are using the same array that we have created in the above example i.e. USStates array. The array will be displayed before and after the sort method.

Experience this example online

You can see in the output of array sort example, the array is sorted after using the sort method of Array class.

In order to sort results in DESC order, you can use reverse method of Array class after the sort method.

C# array length property

The array length property returns the number of elements in an array. We have used this property in the above example with the for loop.

C# array IndexOf

The IndexOf method of array class searches for the given object and returns index value of the first matched element as an integer.

The following example shows how to use the IndexOf C# method. For that example, we have created an array of five elements (USStates). To make it clearer, “Florida” is used twice before using the IndexOf method.

See example by clicking the link below:

Experience this example online

You can see, though we used Florida twice, the IndexOf method only returned index number of the first matched element, which is 2.

C# multidimensional array example

As mentioned earlier, C# supports the multi-dimensional arrays. Following is a C# two-dimensional array example.

We will declare and initialize a two-dimensional array. After assigning values to the elements, we will use two for loops to access those elements and finally display array elements. See example online by clicking the link or image below:

Experience this example online

You can see in the output of above example how each array element is displayed.

See Also: C# list | C# for