Indexes and the bracket notation
The elements of an array are ordered, and each one has a numbered position called its index. To read an element, write the array's name followed by the index in square brackets.
C# arrays are zero-based: the first element is at index 0, the second at index 1, and so on.
string[] animals = {"cat", "dog", "parrot"};
Console.WriteLine(animals[0]); // cat
Console.WriteLine(animals[1]); // dog
Console.WriteLine(animals[2]); // parrot
Note that Console.WriteLine prints a string without quotes. The quotes in source code only mark the string's boundaries.
The last valid index
An array with n elements has the indexes 0 through n - 1. The array above has 3 elements, so its last index is 2, and animals[3] doesn't exist. Counting from the end is covered by the length lesson.
An element is an ordinary value
Reading ages[0] gives you a plain int that you can use anywhere an int fits: in arithmetic, in a comparison, or as an argument to a method. That is why you can add elements directly inside a Console.WriteLine call:
int[] ages = {14, 32, 50, 21};
Console.WriteLine(ages[0] + ages[1]); // 46
The addition happens first and the method receives the result, 46.
Visiting every element
Writing one print statement per element works for a handful of items. For more, a loop can walk through the indexes. The index runs from 0 up to, but not including, the array's length:
string[] fruits = {"mango", "kiwi", "grapefruit"};
for (int i = 0; i < fruits.Length; i++) {
Console.WriteLine(i + ": " + fruits[i]);
}
0: mango
1: kiwi
2: grapefruit
When you don't need the index, foreach (string f in fruits) visits each element in order and gives you its value directly. Both forms read the elements by position, which is exactly what the bracket notation does.
Indexes are whole numbers
The index must be an integer expression. It can be a literal like 1, a variable like i, or a calculation like i + 1. A double index such as fruits[1.0] doesn't compile.
The exercise, step by step
The starter code declares a string array fruits and an int array ages. You add three print statements.
- The second fruit. The second item is at index 1, not 2.
fruits[1]is"kiwi". - The sum of the first three ages. Add
ages[0],ages[1]andages[2]inside the call. That is 14 + 32 + 50 = 96. - The last minus the first. The array has four numbers, so the last is
ages[3], which is 21. Subtractages[0], which is 14, to get 7.
string[] fruits = {"mango", "kiwi", "grapefruit"};
int[] ages = {14, 32, 50, 21};
Console.WriteLine(fruits[1]);
Console.WriteLine(ages[0] + ages[1] + ages[2]);
Console.WriteLine(ages[3] - ages[0]);
The output is:
kiwi
96
7
Common mistakes
- Counting from 1. The "second" item lives at index 1. Using
fruits[2]printsgrapefruitinstead. - Using the count as an index. With four elements,
ages[4]is one past the end and throws anIndexOutOfRangeExceptionat run time. - Parentheses instead of brackets.
ages(0)looks like a method call and doesn't compile. Arrays are indexed with[]. - Putting a string next to a number.
"Sum: " + ages[0] + ages[1]joins text, so it printsSum: 1432, notSum: 46. Wrap the sum in parentheses when you build text.
Why indexing starts at zero
An index is best thought of as an offset: how many steps you move from the start of the array. The first element is zero steps away, the second is one step away, and so on. This is why the first index is 0 and the last is Length - 1. The convention is shared by C#, Java, JavaScript, Python and many other languages, so the habit carries over.
It also explains the off-by-one errors beginners meet. When you count items you start at 1, but when you number positions you start at 0, so the "third" item sits at index 2.
Things to try
Predict each result before running it. For ages from the exercise, ages[3] - ages[2] is 21 - 50, which is -29. For a one-element array such as {7}, the only valid index is 0. And fruits[0].Length is 5, because an element that is a string supports everything a string does, including its own Length.
Key takeaways
- Read an element with
array[index]. - Indexes start at 0 and end at the length minus one.
- An element is a normal value of the array's type, so you can use it in expressions directly.