Skip to main content

How to Get an Item from an Array by Its Index in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Each item in a C# array has a position, its index, and you read an item by putting its index in square brackets after the array. Counting starts at 0, so colors[0] is the first item, and colors[^1] is the last.

Syntax​

Syntax
<array>[<index>]
<array>[^<n>]
<array>.Length
PartWhat it does
<array>[<index>]The item at <index>, counting from 0.
<array>[^<n>]The item <n> places from the end: ^1 is the last item.
<array>.LengthHow many items the array has. The last index is one less.

Microsoft Learn on array access: "If an array index is outside the bounds of the corresponding dimension of an array, the runtime throws an IndexOutOfRangeException." And on the index from end operator: "For example, ^1 points to the last element of a sequence and ^length points to the first element of a sequence."

The Example​

The example reads the first and the third of three colors, the last one with ^1, the array's Length, and then an index past the end.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs:

main.cs, which creates the array red, green, blue and writes colors at 0, at 2, at hat 1, colors.Length and colors at 3.

main.cs
string[] colors = ["red", "green", "blue"];

Console.WriteLine(colors[0]);
Console.WriteLine(colors[2]);
Console.WriteLine(colors[^1]);
Console.WriteLine(colors.Length);
Console.WriteLine(colors[3]);

Step 2: Run It​

Terminal
dotnet run main.cs
Output
red
blue
blue
3
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Program.<Main>$(String[] args) in /Users/Shared/you/main.cs:line 7

A zsh terminal where dotnet run main.cs prints red, blue, blue and 3, then an unhandled System.IndexOutOfRangeException: Index was outside the bounds of the array.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • colors[0] is the first item and colors[2] the third, because counting starts at 0.
  • colors[^1] counted from the end and returned the last item.
  • colors.Length is 3, one more than the last index.
  • colors[3] asked for a fourth item. C# doesn't return a default for a missing position: the program stopped with IndexOutOfRangeException and named the line.

Common Mistakes​

MistakeWhat happensDo this instead
Counting from 1colors[1] is the second itemStart at 0
Using colors[colors.Length] for the last itemIt's one past the end: IndexOutOfRangeExceptionUse colors[^1]
Using a negative index such as colors[-1]warning CS0251: Indexing an array with a negative index (array indices always start at zero), then the same exceptionUse ^1 to count from the end

Troubleshooting​

SymptomLikely causeFix
System.IndexOutOfRangeException: Index was outside the bounds of the array.The index is below 0 or not less than LengthCheck Length before you index
System.NullReferenceExceptionThe array variable is nullMake sure the array was created

FAQ​

Can I change an item by its index?​

Yes: colors[1] = "yellow"; replaces the second item. An array's length is fixed, though, so you can't add an item this way.

What's the difference between an array and a List?​

A List<string> can grow and shrink, and reads items with the same square brackets. Its out-of-range error is an ArgumentOutOfRangeException instead.

Conclusion​

array[index] reads an item, counting from 0, and array[^1] reads the last one. An index outside 0 to Length - 1 throws IndexOutOfRangeException, so check the length when the index comes from data.

Additional Resources​