0Pricing
C# Academy · Lesson

Indexing and Length

Access elements safely.

Indexing and Length is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Changing an Element

You can overwrite any element by assigning a new value to its index.

Here we replace the value at index 1. The array length does not change, only the stored value.

int[] nums = { 5, 6, 7 };
nums[1] = 99;
Console.WriteLine(nums[1]); // 99

The Length Property

Every array has a Length property that tells you how many elements it holds.

This is handy because you do not have to remember the size yourself.

int[] nums = { 5, 6, 7, 8 };
Console.WriteLine(nums.Length); // 4

Last Element

The last valid index is always Length - 1, because counting starts at 0.

Using Length - 1 lets you find the final element without hard-coding a number.

int[] nums = { 5, 6, 7, 8 };
Console.WriteLine(nums[nums.Length - 1]); // 8

The Index From End Operator

C# offers a shortcut for counting from the end using the ^ operator. ^1 means the last element, ^2 the second to last.

It is a clean way to grab the end without writing Length - 1.

int[] nums = { 5, 6, 7, 8 };
Console.WriteLine(nums[^1]); // 8

A Full Program

This complete program prints the first element, the last element, and the total length.

Run it to watch all three values appear.

int[] nums = { 10, 20, 30 };
Console.WriteLine("First: " + nums[0]);
Console.WriteLine("Last: " + nums[nums.Length - 1]);
Console.WriteLine("Count: " + nums.Length);

Out of Range Errors

If you use an index that is too big or negative, C# throws an IndexOutOfRangeException and the program crashes.

For an array of length 3, valid indexes are only 0, 1, and 2. Index 3 is one step too far.

int[] nums = { 1, 2, 3 };
// nums[3] would throw IndexOutOfRangeException
Console.WriteLine(nums[2]); // safe: 3

Checking Before Access

To avoid crashes, check that your index is within range before using it.

A safe index must be at least 0 and less than Length.

int[] nums = { 1, 2, 3 };
int i = 5;
if (i >= 0 && i < nums.Length)
    Console.WriteLine(nums[i]);
else
    Console.WriteLine("Out of range");

Using Length in Math

Because Length is just an integer, you can use it in calculations, such as finding a middle index.

Here we compute the center position of the array.

int[] nums = { 1, 2, 3, 4, 5 };
int mid = nums.Length / 2;
Console.WriteLine(nums[mid]); // 3

Length Is Read-Only

You cannot change Length to resize an array. Arrays have a fixed size once created.

If you need a different size, you create a new array. Collections like List<T> are better when the size must grow.

int[] nums = { 1, 2, 3 };
// nums.Length = 5; // error: read-only
Console.WriteLine(nums.Length); // 3

Quick Check

Test your understanding of indexes and length.

Recap

You learned to read and change elements by index, that indexes are zero-based, and that the last index is Length - 1.

The Length property counts elements, the ^ operator counts from the end, and going out of range throws an exception. Next you will loop over every element.

Frequently asked questions

Is the “Indexing and Length” lesson free?

Yes — the full text of “Indexing and Length” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Indexing and Length”?

Access elements safely. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Indexing and Length” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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