Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The .. (range) operator constructs a System.Range struct that represents a contiguous sub-segment of a collection. It serves as syntactic sugar for defining boundaries, utilizing System.Index types to specify an inclusive lower bound and an exclusive upper bound.

Syntax and Operands

The operator accepts two optional operands. Because both operands are optional, the operator supports four distinct syntax variations:
  • Left operand: The inclusive start index. If omitted, it defaults to 0 (the beginning of the collection).
  • Right operand: The exclusive end index. If omitted, it defaults to ^0 (the end of the collection).
Range explicitBounds = 2..5; // Index 2 (inclusive) to Index 5 (exclusive)
Range openEnd        = 2..;  // Index 2 (inclusive) to the end of the collection
Range openStart      = ..5;  // Beginning of the collection to Index 5 (exclusive)
Range openBoth       = ..;   // The entire collection (0 to ^0)

Type Resolution and Compilation

Operands supplied to the .. operator must evaluate to type int or System.Index. When standard integers are provided, the compiler implicitly converts them to System.Index structs. During compilation, the .. syntax is lowered directly into an instantiation of the System.Range struct via its constructor.
// Source syntax using the range operator (..) and index from end operator (^)
Range range = 1..^1;

// Lowered compiler translation
Index start = new Index(value: 1, fromEnd: false);
Index end = new Index(value: 1, fromEnd: true);
Range loweredRange = new Range(start, end);

Operator Characteristics

  • Associativity: The .. operator is non-associative. Chaining the operator (e.g., a..b..c) is syntactically invalid and produces a compiler error.
  • Precedence: It evaluates with lower precedence than arithmetic operators (like + or *) but higher precedence than relational and equality operators (like < or ==).
  • Immutability: The System.Range struct generated by the operator is a readonly struct. Its Start and End properties cannot be mutated after initialization.
Master C# with Deep Grasping Methodology!Learn More