Hit enter after type your search item

The Arrays in C#

The array is data structures in C# that can store multiple variables of the same type. For example, you want to store the product_id of the products or roll numbers of the students that are integer types.

One way can be declaring and storing the values in the primitive type variables. The other way can be creating an array that can store n number of int type variables for storing the product_id or roll_no etc.

The next sections describe how to create, initialize and use arrays. I will also show you examples of various array types; single dimensional, multi-dimensional and Jagged arrays.

How to create and initialize an Array in C#?

The general syntax of creating/declaring an array is:

type[] NameOfArray;

For example:

int[] intArray;

string[] strArray;

The array can be initiated at the time of declaration. For example:

int[] intArray = new int[] { 5, 10, 14, 21 };

string[] strArray = new string[] {“abc”,”def”,”ghi”,”klm”};

An example of declaring, initiating and accessing array elements

Let us start by a simple example of declaring and initializing an int type array of five elements. This is followed by using a for loop with array length property to access and display the array elements:

The result:






The example of C# string array

The example below creates an array of string type and then its items are accessed by using for loop, array length property and displayed on the screen:

The code:

The output:

c sharp array-string

Using foreach loop to iterate through array elements example

You may also use foreach loop to iterate through the array elements.

c# array-foreach

The example of using Array sort method

Before showing you how to create multi-dimensional and jagged arrays in C#, let us have a look at a few useful methods and a property of the Array class.

The sort() method is used to sort the elements in one-dimensional array. The example below shows how to use the sort method for primitive type array.

For that, we have an array of byte type with random numbers. Then a foreach loop is used to display the array elements which is followed by using the sort() method.

To see the difference, the array elements are displayed again after sorting the array:

c# array-sort

Sorting only range of items

You may also specify the range of elements to be sorted by providing indices. For example, if we have the same array like above example:

byte[] bteArrSort = new byte[] { 99, 8, 5, 11, 21, 4 };

We require to sort it from second to fourth elements only then the sort() method can be used as follows:

Array.Sort(bteArrSort, 1, 3);

The result of this should be (if you iterate through this array and display the items):







The example of Array IndexOf method

The IndexOf method of the Array is used to search objects in the one-dimensional array.  The IndexOf returns an integer value of the first matched element in the array.

So, if the specified array has duplicate occurrences, the method returns only the first found element’s position.

The result:

The Position of the element = 3

You may notice, we searched 75 in the array. It occurred twice in the array while IndexOf returned the index position of the first 75 i.e. 3.

You may search a part of the array rather than complete array by specifying the range of elements.

For example, if we used the IndexOf method like this in the above example:

int x = Array.IndexOf(intArr_indexOf, 75, 4);

The result should be:

The Position of the element = 6

As such, we specified to start searching the array from position 4.

An example of Array length property

The length property of the array gets the total number of elements in the specified array. You might have noticed, we used it in our first example in the for loop.

It set the condition part in the for loop as shown below:

        for (int l = 0; l < intArr.Length; l++)





The example below shows the total number of elements in a string array by using the length property of the Array:

The output of the above code is:

Total Number of Elements in String Array = 9

An example of C# multi-dimensional array

C# supports multi-dimensional arrays. The general syntax of creating a 2D array of string type is:

string [,] Arr2D;

Where Arr2D is the array name.

Similarly, a three dimensional array can be declared as follows:

string [ , , ] Arr3D;

The example below shows how to create a two-dimensional array in C#.

The result:

c# array-2D

C# Jagged array example

The array of arrays is called jagged array. C# supports Jagged array that can be declared as follows:

int [][] ArrJagged;

See an example below for declaring, initializing and accessing a jagged array of string type:

The result:





This div height required for enabling the sticky sidebar