Indexing and Length
Access elements safely.
Reading by Index
Each element in an array has a position called an index. You read a value by putting the index in square brackets after the array name.
Indexes start at 0, so the very first element is at index 0.
string[] days = { "Mon", "Tue", "Wed" };
Console.WriteLine(days[0]); // MonZero-Based Counting
Because indexes start at 0, the second element is index 1 and the third is index 2.
This zero-based counting trips up beginners, so remember: index = position minus one.
string[] days = { "Mon", "Tue", "Wed" };
Console.WriteLine(days[1]); // Tue
Console.WriteLine(days[2]); // WedAll lessons in this course
- Declaring Arrays
- Indexing and Length
- Looping Over Arrays
- Multidimensional Arrays