Array Indexing and Access
Vaibhav • September 11, 2025
In the previous articles, we introduced arrays and saw how they are declared and stored in memory. Now, let’s focus on one of the most important operations you can perform with arrays: accessing their elements. In C#, each element in an array is identified by its position, also called its index. Understanding indexing is fundamental because it determines how you read from and write to arrays.
What is an Index?
An index is a number that specifies the position of an element inside an array. In C#,
indexes are zero-based, meaning the first element of an array is at index 0
, the second at 1
, and so on. If you have an
array with n
elements, the last element will always be at index n - 1
.
// Declaring an array
int[] numbers = { 10, 20, 30, 40, 50 };
// Accessing elements by index
Console.WriteLine(numbers[0]); // Output: 10 (first element)
Console.WriteLine(numbers[2]); // Output: 30 (third element)
Console.WriteLine(numbers[4]); // Output: 50 (last element)
Here, numbers[0]
refers to the first element, numbers[2]
refers to the third element, and so on.
Index Out of Range
If you try to access an element outside the valid range of indexes, the runtime will throw an exception called
IndexOutOfRangeException
.
int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[3]); // ❌ Error: IndexOutOfRangeException
The array numbers
has three elements, so valid indexes are 0
, 1
, and 2
.
Trying to access numbers[3]
goes beyond the valid range.
Always ensure that the index you use is between 0
and array.Length - 1
.
Reading and Writing with Indexes
Indexing is not just for reading array elements - you can also assign values to elements using their index.
int[] scores = new int[3];
// Writing values
scores[0] = 95;
scores[1] = 87;
scores[2] = 100;
// Reading values
Console.WriteLine(scores[0]); // Output: 95
Console.WriteLine(scores[1]); // Output: 87
Console.WriteLine(scores[2]); // Output: 100
Each assignment updates a specific memory location in the array, and each access retrieves the value stored there.
Array Length and Index Safety
Every array in C# has a built-in property called Length
that tells you how many
elements it contains. Using this property, you can write safer code by ensuring your indexes are always within
bounds.
int[] numbers = { 5, 10, 15, 20 };
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
This loop iterates from 0
to numbers.Length - 1
and prints each element without ever risking an out-of-range error.
How Indexing Works in Memory
Arrays are stored in a contiguous block of memory. When you access an element at index i
, the runtime computes its location using the formula:
element_address = base_address + (i * size_of_each_element)
- base_address
is where the array starts in memory.
- i
is the index.
- size_of_each_element
is how many bytes each element takes (for example, int
usually takes 4 bytes).
This explains why accessing an array element is very fast - it’s just a matter of arithmetic, not searching.
Indexing with Variables
Indexes don’t have to be constants; they can come from variables or even calculations. This allows arrays to be used dynamically in loops or conditions.
int[] temperatures = { 22, 25, 27, 23, 26 };
int day = 2;
Console.WriteLine($"Temperature on day {day}: {temperatures[day]}");
// Output: Temperature on day 2: 27
Best Practices for Array Indexing
- Always check the array’s
Length
before accessing indexes. - Use loops when working with multiple elements to avoid repetition and mistakes.
- Remember that indexes start at zero - a common source of off-by-one errors.
- Avoid hardcoding indexes unless you’re absolutely sure of the array’s structure.
Relying on Length
makes your code more flexible and less error-prone,
especially when arrays change size.
Summary
Indexing is how you interact with arrays in C#. Each element is accessed through a zero-based index, and knowing
how to read and write values is essential for effective programming. Arrays are stored in contiguous memory
blocks, making element access extremely fast. By using the Length
property and
following best practices, you can safely and efficiently manage data in arrays without running into errors like
IndexOutOfRangeException
.