C# Feature - Range operator
Overview
Section titled βOverviewβThe range operator .. creates slices between two indexes and works especially well with arrays, spans, and strings.
Introduced In
Section titled βIntroduced InβC# 8.0 (2019)
string middle = text.Substring(1, text.Length - 2);string middle = text[1..^1];Annotated Example
Section titled βAnnotated Exampleβint[] lastThree = numbers[^3..];int[] withoutFirst = numbers[1..];Gotchas & Best Practices
Section titled βGotchas & Best Practicesβ- The end index is exclusive.
- Prefer ranges when slicing is the main idea and not just incidental arithmetic.
Related Features
Section titled βRelated FeaturesβVersion Notes
Section titled βVersion Notesβ- C# 8 introduced
Rangeand the..operator.