C# Arrays Declaration, Initialization, Assignment and Loops

An array is a collection of elements of same type. Arrays are used to store group of variables. C# array is zero indexed. The size and rank of the array are fixed when the array is created.

Types of Arrays

Single Dimensional Arrays - Most standard form used to store a set of sequential elements.

Multidimensional Arrays - Used to store n-dimensional data like Matrices.

Jagged Arrays - Array of Arrays of same or different dimensions.

Declaring Arrays

Arrays are declared using the below Syntax.

                dataType[ ] nameOfTheArray

                dataType – The type of array

                nameOfTheArray – Name of the array variable

                [] – Rank of the array

        // Declare a one dimensional array of integers whose initial size is 10.
        int[] numbers = new int[10];

        //Declare a two dimensional array of strings. Rank1 (rows) size is 4 and Rank2(columns) size is 5.
        string[,] stringsArray = new string[4,5];

        // Declare a Jagged int array of size 5. Each element in the Jagged array
        // can hold an integer array of any dimension.
        int[][] myJagArray = new int[5][];


Initializing Arrays

                Arrays are usually initialized at the time of declaration by using the curly bracket syntax. If the initialization is not done at the time of declaration, array values are initialized to the default values of the array type.

    // Declare and initializes an integer array of size 5 with initial values.
    int[] numbersInitialized = new int[]{1,2,5,4,8}; 

    // Declares and initializes a 2 dimensional array string array.
    string[,] myStrings = new string[,] {{"row1", "col1"},{"row2","col2"}};

    // Declares and initializes an integer array of size 3. The size in the bracket is optional.
    int[] numbersInitialized1 = new int[3] {4, 8,3};

Assigning and Accessing Array Elements

                Array elements are accessed using the index and array variable name. Array name with index is used on left side of the expression to assign a value to the element. Accessing an element is done by using the array variable and index on the right size of the expression.

    // Assigns the element at index 1 to 10.
    numbersInitialized[1] = 10; 
    // Retrieving the element at index 2 and storing in a variable named number.
    int number = numbersInitialized[2];

    // Retrieving the element at row 1 and column 2 in a two dimensional string array.
    string element = myStrings[0, 1];

    // Initializing jagged array.
    for (int i = 0; i < myJagArray.Length; i++)
        // The array size is i + 2.
        myJagArray[i] = new int[i + 2];

        for (int j = 0; j < myJagArray[i].Length; j++)
            myJagArray[i][j] = j + 1;

Looping through Array Elements

                Looping through each element of an array and performing calculations on its data is a common operation. There are two ways to loop through the array.

For Loop

                Array elements can be looped using the standard for loop. Length property of the array is used to check the end condition.

// Loop through the array using for statement and
    // print the elements to Console.
    for (int i = 0; i < numbersInitialized.Length; i++)

    // Loop through the two dimensional array of size [2,2] and 
    // print elements.
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            Console.Write(myStrings[i,j] + " ");

    // Looping through Jagged array
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < myJagArray[i].Length; j++)
            Console.Write(myJagArray[i][j] + " ");

Foreach Loop

                C# arrays provides enumerator which is required to use foreach loop.

    int[] myNumbers = { 3, 6, 8 };

    // Loop through the array and print the elements.
    foreach (int index in myNumbers)

System.Array Base Class

Arrays are inherited from Syste.Array base class which provides some useful methods.

Below tables lists some of the important members of System.Array. Please refer to Microcoft.NET documentation for complete list of members.




Sets the range of elements in an array to default value of its type. 0 for numbers, null for reference type and false for Booleans.


Reverses the elements in a one dimensional array.


Sorts the elements in a one dimensional array. Custom types need to implement IComparer.


Gets the value at a specific position.


This property returns the number of items in the array.