CKTIS / C#
System.Index and System.Range: positions and slices
Resolve positions from either end of a sequence, validate slice boundaries, and tell when slicing copies elements or returns a view.
Version: C# 12 and .NET 8; later runtimes may add compatible features
Syntax
System.Index fromStart = 1;
System.Index fromEnd = ^1;
System.Index explicitIndex = new System.Index(1, fromEnd: true);
System.Range range = fromStart..fromEnd;
System.Range explicitRange = new System.Range(fromStart, fromEnd);
System.Range all = ..;
int offset = fromEnd.GetOffset(length);
(int start, int count) = range.GetOffsetAndLength(length);
var item = sequence[fromEnd];
var slice = sequence[range];In C# 12 and .NET 8, System.Index and System.Range are immutable value types in the System namespace. They describe positions without holding data or a reference to a sequence. Index i = 1 uses an implicit conversion from int and counts from zero at the start. Index i = ^1 is equivalent to new Index(1, fromEnd: true) and selects the last element of a nonempty sequence. The prefix ^n is not a ^ b, which computes bitwise XOR for integers and logical XOR for bool.
i.GetOffset(length) returns i.Value or, when i.IsFromEnd is true, length - i.Value. It does not check whether an element exists at that offset. ^0 is the position just past the last element: a valid range boundary, but reading array[^0] throws IndexOutOfRangeException.
Range holds two Index values: the inclusive Start and the exclusive End. start..end is equivalent to new Range(start, end). Omitted boundaries are 0 and ^0, respectively, so .. describes the whole sequence. For a given sequence length, GetOffsetAndLength(length) returns (Offset, Length). Equal offsets give an empty slice; reversed boundaries or offsets outside [0, length] cause ArgumentOutOfRangeException.
Support for sequence[i] and sequence[range] depends on the target type. C# supports one-dimensional arrays and string; another type can expose a matching indexer or meet the compiler pattern: an accessible int Length or int Count plus an int indexer for Index or Slice(int, int) for Range. In the example, the array slice copies elements and the Span<T> slice views the same storage. In C# 12, you cannot use Span<T> variables in async methods; that is a ref struct restriction, not a Range restriction.
Examples
using System;
int[] data = { 10, 20, 30, 40, 50 };
Index last = ^1;
Range window = 1..last;
(int offset, int length) = window.GetOffsetAndLength(data.Length);
int[] copy = data[window];
Span<int> span = data.AsSpan();
Span<int> view = span[window];
view[0] = 99;
Console.WriteLine(FormattableString.Invariant($"last={data[last]}"));
Console.WriteLine(FormattableString.Invariant($"offset={offset}, length={length}"));
Console.WriteLine(FormattableString.Invariant($"copy={copy[0]}, source={data[offset]}, view={view[0]}"));Result
last=50 offset=1, length=3 copy=20, source=99, view=99
`^1` selects `50`, but `1..^1` excludes it and includes `20`, `30`, and `40`. Writing `99` through the view changes the source array but not the copy.
Related lessons
- C# · Lesson 51 · Advanced · Version: 3