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;For example, the following declares an array of integers:
int[] scores;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];Also, you can both declare and create a new array using a single statement like this:
int[] scores = new int[5];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;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 };Or simply:
int[] scores = { 3, 2, 5, 4, 1 };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.LengthIterating 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]);
}Output:
3
2
5
4
1Hence, 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]} ");
}
}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]); // 4Output:
1
4The 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]); // 4Ranges #
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]The following returns a new array that stores all the elements starting from the N element to the end of the array:
arrayName[N..]The following returns a new array that stores the elements from the N to M element:
arrayName[N..M]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);Output:
scores:
3 2 5 4 1
scores[..2]:
3 2
scores[2..]:
5 4 1
scores[1..3]:
2 5The 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]);Summary #
- An array holds a fixed number of elements of the same type.
- An array is a zero-based index.
- Use the
Lengthproperty to access the size of an array. - Use the
forstatement 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.
Thank you for your feedback!