Accessing Array Elements
In C#, you access elements of an array using the square bracket notation. Remember that array indexing is zero-based. This means that:
myArray[0]
references the first element in the array.myArray[2]
references the third element in the array.
You can both retrieve and set the value of an element using this notation:
Using Loops to Access Array Elements
Loops are ideal for accessing each element in turn. The following for
loop prints each element of the array:
myArray.Length
is a property that returns the number of elements in the array. Here, it would return6
.- The loop runs from
i = 0
toi < myArray.Length - 1
, ensuring we do not exceed the bounds of the array.
Array Bounds and Exceptions
- Lower Bound: The lowest index of an array is always
0
. - Upper Bound: The highest index is always
myArray.Length - 1
. - Out-of-Bounds Access: Trying to access an index outside this range (e.g.,
myArray[6]
ormyArray[-1]
) will result in anIndexOutOfRangeException
. This is an error indicating that you are trying to access a memory location not allocated to the array.
Note
You can use expressions within the square brackets, such as myArray[i + 1]
or myArray[12 / 4 - 1]
, as long as the result is a valid index.
Using the foreach
Loop
The foreach
loop is a convenient way to iterate through all elements in an array:
- The
foreach
loop processes each element in the array, from the first to the last. - The loop variable
element
is read-only within the loop and represents the current array element. - This loop is ideal for read-only access but cannot be used to modify the array elements directly.
Additional Tips
- Empty Arrays: An array can be declared with a size of zero, but accessing any element will throw an
IndexOutOfRangeException
. - Null Arrays: If an array is not initialized, attempting to access it will result in a
NullReferenceException
. Always check if an array is initialized before use.