In programming, a variable is used to store a value in memory. Arrays are a type of variable that is able to store multiple values. Arrays can be thought of as a container of values. This article will explain how to use arrays in C#.
In C#, arrays must be of the same data type like int, double, string, or char.
Declaring An Array
Below is an example of how to declare an array of integers.
int[] nums = { 1, 2, 3 };
The above example works like this:
- int[] specifies that the variable will be an array of integers.
- nums is the name of the array variable.
- The values of 1, 2, and 3 that are in the curly braces are assigned to the nums array.
The below screenshot shows the nums array in the Visual Studio debugger.

The above screenshot shows that the values (elements) of 1, 2, and 3 are assigned to the corresponding indexes of 0, 1, 2. This is a very important point to be aware of that the indexes of arrays start at 0 instead of 1. The indexes will be used to access the elements of the array.
Below is an example of how to declare an array of strings and use it to store names.
string[] names = { "John", "Jane", "Bob", "Sally"};
The below screenshot shows this array of strings and the values assigned to the indexes.

It is common to not know what values will be stored in an array when it is created. The below example shows how to create an array of integers that does not have the values initialized during creation of the array.
int[] nums = new int[3];
The above line of code works like this:
- int[] specifies that the variable will be an array of integers.
- nums is the name of the array variable.
- The new keyword followed by int followed by brackets [] tells the compiler to create a new array of integers.
- The number 3 in brackets in int[3] specifies that the array will hold 3 integers. This is important because the array’s size is set at declaration and can not be changed later.
Below is a screenshot of the above line of code in the Visual Studio debugger. The screenshot shows that the nums array holds three integer values. It also shows that since the integers values were not given during the declaration of the nums array that the values are set to zero by default.

Assigning And Accessing Array Elements
To assign values to the indexes of an array you enter the value of the index in brackets in an assignment statement. The below code shows an example.
int[] nums = new int[3]; nums[0] = 1; nums[1] = 2; nums[2] = 3;
Variables can also be used to specify the index of the array to be used like the the below example.
int[] nums = new int[3]; int i = 0; nums[i] = 1;
The index is also used to access the elements of an array. The below code will access the nums array at index 1 which has the value of 2 stored so the output will be 2.
int[] nums = new int[3]; nums[0] = 1; nums[1] = 2; nums[2] = 3; Console.Write(nums[1]);
Avoiding Index Out Of Range Exceptions
If you try to access an element that is outside of the set range of indexes then a Index Out Of Range exception will be thrown.
int[] nums = new int[3]; nums[0] = 1; nums[1] = 2; nums[2] = 3; Console.Write(nums[3]);
The above code will cause the following error message: Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. This is because an index of 3 is attempted to be accessed and because array indexes start at 0 the range of available indexes for this array is 0 through 2.
A way to get the last element of an array and avoid this exception is to get the length of array using the length property and then subtract one from it.
int[] nums = new int[3]; nums[0] = 1; nums[1] = 2; nums[2] = 3 Console.WriteLine(nums[nums.Length - 1]);
The output of the above code is 3. This code uses the length property in nums.Length which is three and then subtracts one from it because the array index starts at zero.
Looping Through An Array With A for Loop
The below code is an example of using a for loop to loop through an array.
int[] nums = new int[3]; nums[0] = 1; nums[1] = 2; nums[2] = 3; for (int i = 0; i < nums.Length; i++) { Console.WriteLine(nums[i]); }
The above code uses a for loop that sets the variable i to 0 to access the first element of the array and then accesses each element of the array until the loop ends when i is incremented to the length of the array.
Looping Through An Array With A foreach Loop
A foreach loop can be used to loop through an array without using an index. The below code is an example of using a foreach loop to loop through an array.
int[] nums = new int[3]; nums[0] = 1; nums[1] = 2; nums[2] = 3; foreach (var num in nums) { Console.WriteLine(num); }
Conclusion
An array is a type of variable that can hold multiple values. C# arrays hold values of the same type and have a fixed length that is set at declaration.
References:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays
https://www.tutorialspoint.com/csharp/csharp_arrays.htm
https://codebuns.com/csharp-intermediate/single-dimensional-array/
https://www.tutorialsteacher.com/csharp/array-csharp