C# Arrays Tutorial For Beginners With Examples

A C# Array is used to store same type of data.
Arrays can contain 'n' number of items and these items are accessed using an index. Arrays are zero indexed so an array with n elements starts with zero and ends with n-1.

Categories of Arrays in C#

Arrays can be categorized as below:
  • Single-Dimensional Arrays
  • Multidimensional Arrays
  • Jagged Arrays
In this chapter Single-Dimensional Arrays will be discussed. Multidimensional Arrays and Jagged Arrays will be discussed in the Advanced section of this tutorial.

When to use Arrays

Arrays are useful when the requirement is to create variables containing similar type of data.
For example, if the requirement is to create different variables to store animal names then it is beneficial to create an array of string type and store the animal names in this array.



How to declare an Array

Arrays are declared using square brackets. Below example shows how to declare different type of arrays:
    string[] animalNames;

    int[] months;

    double[] starNumbers;
        

Initializing an Array

To initialize arrays new keyword has to be used. Once initialized the default value for numeric type array elements is zero and for reference type array elements is null.
    animalNames = new string[5];

    months = new int[12];

    starNumbers = new double[210000000];
        
Declaration and initialization can be done in a single statement as below.
    string[] animalNames = new string[5];    
        

Storing values in an Array

Once initialized values can be stored in arrays. There are different ways to store or assign values to arrays.
    string[] animalNames = new string[5];    

    animalNames[1] = "Tiger";
        
Values can also be stored in arrays at the time of declaration.
    string[] animalNames = {"Cat", "Tiger", "Elephant"};

    string[] animalNames = new string[3] {"Cat", "Tiger", "Elephant"};

    string[] animalNames = new string[] {"Cat", "Tiger", "Elephant"};
        

Getting values from an Array

Array in C# is index based. To store values or to access these values, index has to be used with the array name. In the above example we have stored the Animal names in the array animalNames. To retrieve these animal names the below code can be used.
    Console.WriteLine("First animal name in the array is - " + animalNames[0]);

    Console.WriteLine("Second animal name in the array is - " + animalNames[1]);

    Console.WriteLine("Third animal name in the array is - " + animalNames[2]);
        

C# Multidimensional Arrays

The arrays explained above have one dimension or are Single-Dimensional. In C# arrays having more than one dimension can also be created. These type of arrays are called as Multidimensional arrays.
These arrays will be covered in the advanced section of this tutorial.

C# Jagged Arrays

Arrays whose elements are also arrays are called as Jagged Arrays.
More about this will be covered in the advanced section of this tutorial.