0Pricing
C# Academy · Lesson

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]); // Mon

Zero-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]); // Wed

All lessons in this course

  1. Declaring Arrays
  2. Indexing and Length
  3. Looping Over Arrays
  4. Multidimensional Arrays
← Back to C# Academy