C# Reference ยท Types and Literals
The Index and Range types
Reference for System.Index and System.Range in C#: the ^ from-end operator, slicing with .., supported types, custom support and common mistakes.
System.Index represents a position in a sequence, counted from the start or from the end. System.Range represents a pair of indexes, a start and an end. Two operators create them: ^n means "n from the end" (so ^1 is the last element) and a..b means "from a up to, but not including, b".
They make slicing short and readable: text[..3], items[^2..], array[1..^1]. Arrays, strings, spans and List<T> indexing all understand them, and you can add support to your own types.
int[] data = [10, 20, 30, 40, 50]; Console.WriteLine(data[^1]); Console.WriteLine(string.Join(",", data[1..3]));
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
public readonly struct Index : IEquatable<Index> public readonly struct Range : IEquatable<Range> Index i = ^2; // same as new Index(2, fromEnd: true) Range r = 1..^1; // new Range(1, ^1) Range all = ..; // Range.All
| Member | Description |
|---|---|
Index.Start, Index.End |
0 and ^0. |
Index.Value, IsFromEnd |
The number and whether it counts from the end. |
Index.GetOffset(length) |
Converts to a zero-based position for a collection of that length. |
Range.Start, Range.End |
Two Index values. |
Range.GetOffsetAndLength(length) |
Returns (Offset, Length); throws ArgumentOutOfRangeException if the range does not fit. |
Range.All, Range.StartAt(i), Range.EndAt(i) |
Convenience factories. |
Errors: negative values passed to new Index(value) throw ArgumentOutOfRangeException; indexing past the real bounds throws IndexOutOfRangeException (single index) or ArgumentOutOfRangeException (range).
Usage notes
^0is one past the last element. It is valid as the end of a range (data[2..^0]) but not as an element index.- The end is exclusive, the start inclusive:
data[1..3]has two elements. - Arrays copy on slice, strings produce a new string, and spans produce a view without copying.
- Support for custom types is by pattern. If a type has a
CountorLengthproperty and anintindexer,^njust works. If it has a methodSlice(int start, int length), range expressions work too. The compiler rewrites the expression for you. - Evaluation:
data[^1]is rewritten asdata[data.Length - 1], so the collection expression is evaluated once. List<T>accepts^nfor single elements. For slices uselist[1..3], which returns a newList<T>; itsGetRangeis the older equivalent.- Dictionaries and
IEnumerable<T>have no index support. LINQElementAt(^1),Take(1..3)andTake(^2..)add it for sequences. - Strings slice by UTF-16 code unit, which can split a surrogate pair. See Unicode text.