0Pricing
C# Academy · Lesson

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..3

The .. (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,30

All lessons in this course

  1. The Index Type and ^ Operator
  2. The Range Type and .. Operator
  3. Ranges with Arrays and Strings
  4. Custom Types Supporting Ranges
← Back to C# Academy