The Range Type and .. Operator
Take slices with range expressions.
Meet the Range Type
System.Range describes a slice of a sequence using a start and an end Index. It pairs perfectly with the range operator .. to express sub-sequences.
using System;
Range r = 1..3;
Console.WriteLine(r.Start + ".." + r.End); // 1..3The .. (range) Operator
The range operator a..b builds a Range from index a (inclusive) to index b (exclusive). So 1..3 covers positions 1 and 2.
using System;
int[] data = { 10, 20, 30, 40, 50 };
int[] slice = data[1..3];
Console.WriteLine(string.Join(",", slice)); // 20,30All lessons in this course
- The Index Type and ^ Operator
- The Range Type and .. Operator
- Ranges with Arrays and Strings
- Custom Types Supporting Ranges