Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Index from End: Using the ^ Operator

Use the C# ^ operator to access elements from the end of arrays, lists, strings, and spans. See examples, edge cases, and performance notes.

~5 min readC#
#C##Index#Range#Arrays
Illustration of a C# array with an arrow pointing to the last element using the ^1 index-from-end syntax.

The index-from-end feature, introduced in C# 8.0, lets you access elements relative to the end of a collection using the ^ operator. Instead of calculating array.Length - 1 manually, you write array[^1] to get the last element. This syntax is concise and reduces off-by-one errors, but it also introduces the System.Index type and a few behavioral details worth understanding before you use it in production code.

The ^ Operator and the Index Type

The ^ operator in an index expression is shorthand for the System.Index struct. When you write ^n, the compiler creates an Index that represents a position counted from the end. The value ^1 means the last element, ^2 means the second-to-last, and so on. This is different from a zero-based index from the start, where 0 is the first element.

csharp
int[] numbers = { 10, 20, 30, 40, 50 }; int last = numbers[^1]; // 50 int secondLast = numbers[^2]; // 40

The Index struct has an IsFromEnd property and a Value property. When IsFromEnd is true, Value indicates the offset from the end. The compiler translates ^n into new Index(n, fromEnd: true). You can also create an Index explicitly if you need to store it or pass it around.

csharp
Index lastIndex = ^1; int value = numbers[lastIndex];

How Index from End Works Under the Hood

When you use an Index with a collection that has an integer indexer, the compiler must convert the Index into an actual integer index. The conversion uses the collection's Length or Count. For an array of length n, ^i resolves to n - i. So ^1 becomes n - 1, ^2 becomes n - 2, and so on. This means that ^0 would resolve to n, which is out of bounds for a zero-based collection. The compiler does not prevent you from writing ^0, and at runtime the resulting element access fails. For an array, that is an IndexOutOfRangeException; for a List<T>, it is an ArgumentOutOfRangeException.

csharp
int[] numbers = { 1, 2, 3 }; // This throws: IndexOutOfRangeException int invalid = numbers[^0];

Using Index from End with Arrays and Lists

Arrays and List<T> are the most common places to use index-from-end syntax. For arrays, the behavior is straightforward and efficient because the index is computed directly from Length. For List<T>, the compiler translates the expression by using Count before calling the list's int-based indexer. The extra calculation is small and typically does not cause allocations.

csharp
List<string> names = new List<string> { "Alice", "Bob", "Carol" }; string firstFromEnd = names[^3]; // "Alice" string lastFromEnd = names[^1]; // "Carol"

You can also use Index variables to avoid repeating the same offset. This is useful when you need to access the same element multiple times, especially if the collection length might change between accesses.

csharp
Index secondLast = ^2; int[] data = { 5, 6, 7, 8 }; int value = data[secondLast]; // 7

Using Index from End with Strings and Spans

Strings support index-from-end syntax as well. This is handy for extracting a substring from the end without calculating the start index manually. For example, to get the last character of a string, you can use str[^1]. To get a range from the end, combine ^ with the range operator ...

csharp
string path = "/home/user/file.txt"; char lastChar = path[^1]; // 't' string fileName = path[^8..]; // "file.txt" (last 8 characters)

The Range operator (..) works with Index values on both ends. ^8.. means from 8 positions from the end to the end of the string. This is equivalent to path.Substring(path.Length - 8). The range syntax is compiled into a System.Range struct, which can be used with arrays, strings, and spans.

Span<T> and ReadOnlySpan<T> also support Index and Range. This is particularly useful for high-performance code that avoids allocations. For example, you can slice a span without creating a new array.

csharp
Span<int> numbers = stackalloc int[] { 1, 2, 3, 4, 5 }; Span<int> lastTwo = numbers[^2..];

Performance and Allocation Considerations

Index-from-end syntax does not inherently cause allocations. Index and Range are structs, and for arrays, strings, and spans, the index calculation is usually simple arithmetic. For List<T> and other countable collections, the compiler uses the collection's Count to calculate the index; this is a small amount of work and is not a meaningful bottleneck in most code.

One area where you should be careful is using ranges with arrays. When you use a range like array[1..^1], the compiler creates a new array containing the sliced elements. This is a copy operation and can be expensive for large arrays. If you need to avoid copying, use Span<T> or Memory<T> instead.

csharp
int[] largeArray = Enumerable.Range(0, 1000000).ToArray(); // This copies a million elements into a new array int[] slice = largeArray[100..^100]; // This creates a span over the same memory without copying Span<int> spanSlice = largeArray.AsSpan()[100..^100];

For strings, slicing with a range also creates a new string. If you only need to read a portion of the string, consider using AsSpan() and then slicing the span to avoid the allocation, especially in hot paths.

Common Pitfalls and Edge Cases

One common mistake is using ^0 thinking it refers to the first element from the end. It actually refers to the position after the last element, which is out of bounds. To get the last element, use ^1; to get the second-to-last, use ^2; and so on.

Another limitation is that ^ works with types that have an indexer and a Length or Count property. For example, LinkedList<T> has no integer indexer, so you cannot write linkedList[^1]; this is a compile-time error, not a runtime exception.

When using ranges, be aware that the end index is exclusive. array[1..^1] includes elements from index 1 up to but not including the last element. This is consistent with the rest of C# range behavior.

csharp
int[] numbers = { 0, 1, 2, 3, 4 }; int[] middle = numbers[1..^1]; // { 1, 2, 3 }

Compatibility and Language Version Requirements

The ^ operator and the Index/Range types require C# 8.0 or later. If you are working with an older codebase, you may need to upgrade the language version in your project file. Additionally, the target framework must include the System.Index and System.Range types. These types are included in .NET Core 3.0 and later, including .NET 5 and later. For .NET Framework, you may need to use a compatibility package or target a newer runtime.

When using index-from-end syntax in a library that targets multiple frameworks, ensure that the target framework includes these types. You can add a compatibility package for older frameworks, but it is simpler to target a modern .NET version if possible.

xml
<PropertyGroup> <LangVersion>latest</LangVersion> <TargetFramework>net8.0</TargetFramework> </PropertyGroup>

The Index and Range types are also used by the Range operator, which is essential for slicing. If you are using a framework that does not support these types, the compiler will produce errors. Verify that your target environment supports C# 8.0 features before adopting this syntax in production code.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.