C# Reference ยท Operators and Expressions
C# index-from-end ^ and range .. operators
Reference for the C# index-from-end operator ^ and range operator ..: slicing arrays, strings, spans and lists, Index and Range types, and custom support.
Two operators from C# 8 make slicing short. The index-from-end operator ^n counts from the end of a sequence: ^1 is the last element. The range operator start..end describes a slice: it includes start and excludes end.
Both are syntax for the System.Index and System.Range types. They work on arrays, strings, Span<T>, List<T> (indexing) and any type that exposes a Length or Count and a suitable indexer or Slice method. Either side of .. may be omitted: ..3 is "from the start", 2.. is "to the end", .. is everything.
int[] a = { 10, 20, 30, 40, 50 }; Console.WriteLine(a[^1]); // 50 Console.WriteLine(string.Join(",", a[1..3])); // 20,30
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
Index fromEnd = ^n; Range r = start..end; element = sequence[^n]; slice = sequence[start..end];
| Form | Meaning |
|---|---|
^n |
Index n positions from the end; ^1 is the last element, ^0 is one past the last (valid only as a range end). |
a..b |
Range from a (inclusive) to b (exclusive). |
..b, a.., .. |
Open start, open end, or everything. |
x[^n] |
Element Length - n. |
x[a..b] |
A slice of b - a elements. |
Returns: ^n produces an Index; a..b produces a Range. Applied to an array, x[range] returns a new array (a copy). On a string it returns a new string; on Span<T>/ReadOnlySpan<T> it returns a span over the same memory.
Errors: a bad range on an array, string or span throws ArgumentOutOfRangeException, and so does a reversed range such as 3..1. A bad ^n index throws IndexOutOfRangeException on arrays and strings and ArgumentOutOfRangeException on List<T>.
Usage notes
- Arrays copy, spans share. Slicing an array allocates a new array. To avoid copying, slice a span:
array.AsSpan(1..3)ornew ReadOnlySpan<int>(array)[1..3]. - Strings.
s[2..5]isSubstring(2, 3). A singles[^1]returns achar. - Make your own type support them. Implicit index support: expose
LengthorCountplus anintindexer, andx[^1]works. Implicit range support: also exposeSlice(int start, int length), andx[a..b]works. Alternatively, declare indexers that takeIndexorRangedirectly. List<T>supportslist[^1](it hasCountand anintindexer) but has noSlicemethod, solist[1..3]does not compile. Uselist.GetRange(1, 2)orCollectionsMarshal.AsSpan(list)[1..3].- LINQ.
Enumerable.Take(1..3)andTake(^3..)accept ranges (.NET 6+).ElementAt(^1)accepts an index. - Evaluation:
a[x..y]evaluatesaonce, then computes the offsets using the length. - Off by one. The end is exclusive, but
^1refers to the last element. Soa[..^1]is "all but the last", anda[^1..]is "just the last". - Not the same as the
..spread in collection expressions ([..a, ..b]); that syntax is covered in Collection expressions and spread. - Performance: index arithmetic is free; slicing an array or string costs an allocation and a copy proportional to the slice length.