Skip to content
Tutorials Center
My learning

Topics

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.

C#
int[] a = { 10, 20, 30, 40, 50 };
Console.WriteLine(a[^1]);              // 50
Console.WriteLine(string.Join(",", a[1..3]));   // 20,30

Try it

C# Indexes and ranges

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.

C# Copies, spans, lists and errors

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

C#
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) or new ReadOnlySpan<int>(array)[1..3].
  • Strings. s[2..5] is Substring(2, 3). A single s[^1] returns a char.
  • Make your own type support them. Implicit index support: expose Length or Count plus an int indexer, and x[^1] works. Implicit range support: also expose Slice(int start, int length), and x[a..b] works. Alternatively, declare indexers that take Index or Range directly.
  • List<T> supports list[^1] (it has Count and an int indexer) but has no Slice method, so list[1..3] does not compile. Use list.GetRange(1, 2) or CollectionsMarshal.AsSpan(list)[1..3].
  • LINQ. Enumerable.Take(1..3) and Take(^3..) accept ranges (.NET 6+). ElementAt(^1) accepts an index.
  • Evaluation: a[x..y] evaluates a once, then computes the offsets using the length.
  • Off by one. The end is exclusive, but ^1 refers to the last element. So a[..^1] is "all but the last", and a[^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.