C# Array

Summary: in this tutorial, you’ll learn about the C# array and how to use it to store a fixed number of values of the same type.

Introduction to the C# array

An array stores a fixed number of elements of the same type. To declare an array variable, you use square brackets [] after the element’s type:

type[] arrayName;Code language: C# (cs)

For example, the following declares an array of integers:

int[] scores;Code language: C# (cs)

To create an array, you use the new keyword with the element’s type and the number of elements that the array will hold.

For example, the following declares an array variable and creates a new array that holds five integers:

int[] scores;
scores = new int[5];Code language: C# (cs)

Also, you can both declare and create a new array using a single statement like this:

int[] scores = new int[5];Code language: C# (cs)

Since we haven’t initialized the elements of the scores array, they will take the default value of the int type, which is zero.

Accessing array elements

To access an element of an array, you use the square bracket [] with an index. An array is zero-based indexing. It means that the index of the first element starts at zero, the index of the second element starts at one, and so on.

If an array has N elements, the index of the last element is N – 1. The following example assigns an integer to each array’s element in the scores array:

scores[0] = 3;
scores[1] = 2;
scores[2] = 5;
scores[3] = 4;
scores[4] = 1;Code language: C# (cs)

Initializing an array

To declare and populate an array in a single statement, you can use the array initialization expression. The following example defines the scores array and initializes its elements:

int[] scores = new int[5] { 3, 2, 5, 4, 1 };Code language: C# (cs)

Or simply:

int[] scores = { 3, 2, 5, 4, 1 };Code language: C# (cs)

Getting array size

The Length property returns the size of an array. To access the Length property, you use the array variable with the dot operator and Length property. For example:

scores.LengthCode language: C# (cs)

Iterating over array elements

To iterate over elements of an array, you use the for statement. The following example uses the for statement to output the elements of the scores array to the console:

int[] scores = { 3, 2, 5, 4, 1 };

for (int i = 0; i < scores.Length; i++)
{
    Console.WriteLine(scores[i]);
}Code language: C# (cs)

Output:

3
2
5
4
1Code language: C# (cs)

Hence, you can define a function that outputs an array of integers to the console like this:

void PrintArray(int[] items)
{
    for (int i = 0; i < items.Length; i++)
    {
        Console.Write($"{items[i]} ");
    }
}Code language: C# (cs)

Indices

Indices allow you to reference the elements relative to the end of the array with the ^ operator and an index. The ^1 references the last element, ^2 references the second-to-last element, and so on.

The following example uses indices to access the last and the second-to-last elements of the scores array:

int[] scores = { 3, 2, 5, 4, 1 };

Console.WriteLine(scores[^1]); // 1
Console.WriteLine(scores[^2]); // 4Code language: C# (cs)

Output:

1
4Code language: C# (cs)

The Index type allows you to access an array element using a variable instead of a literal integer. For example:

int[] scores = { 3, 2, 5, 4, 1 };

Index lastIndex = ^2;
Console.WriteLine(scores[lastIndex]); // 4Code language: C# (cs)

Ranges

Ranges allow you to slice an array using the .. operator.

The following returns a new array that stores the first N elements in an array.

arrayName[..N]Code language: C# (cs)

The following returns a new array that stores all the elements starting from the N element to the end of the array:

arrayName[N..]Code language: C# (cs)

The following returns a new array that stores the elements from the N to M element:

arrayName[N..M]Code language: C# (cs)

See the following example:

void PrintArray(int[] items)
{
    for (int i = 0; i < items.Length; i++)
    {
        Console.Write($"{items[i]} ");
    }
    Console.WriteLine();
}

int[] scores = { 3, 2, 5, 4, 1 };

Console.WriteLine("scores:");
PrintArray(scores);


// get the first two elements
int[] subScores = scores[..2];
Console.WriteLine("scores[2..]:");
PrintArray(subScores);

// get the elements starting from the 3rd element
subScores = scores[2..];
Console.WriteLine("scores[2..]:");
PrintArray(subScores);

// get all the elements starting from the 2nd element
Console.WriteLine("scores[1..3]:");
subScores = scores[1..3];
PrintArray(subScores);Code language: C# (cs)

Output:

scores:
3 2 5 4 1
scores[..2]:
3 2
scores[2..]:
5 4 1
scores[1..3]:
2 5Code language: C# (cs)

The Range type allows you to slice an array using a variable. For example:

void PrintArray(int[] items)
{
    for (int i = 0; i < items.Length; i++)
    {
        Console.Write($"{items[i]} ");
    }
    Console.WriteLine();
}

int[] scores = { 3, 2, 5, 4, 1 };

Range r = 1..3;

PrintArray(scores[r]);Code language: C# (cs)

Summary

  • An array holds a fixed number of elements of the same type.
  • An array is a zero-based index.
  • Use the Length property to access the size of an array.
  • Use the for statement to iterate over elements of an array.
  • Use indices (^index) to access elements from the end of an array.
  • Use ranges to slice an array.
  • Do use an array if the number of elements is known at the design time. Avoid using it if the size of the array is now known.
Was this tutorial helpful ?