The Range Type and .. Operator
Take slices with range expressions.
The Range Type and .. Operator is a free C# Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.
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,30Start Inclusive, End Exclusive
The exclusive end mirrors how for loops work: the number of elements equals end - start. Range 2..5 yields three elements.
using System;
int[] data = { 0, 1, 2, 3, 4, 5, 6 };
int[] slice = data[2..5];
Console.WriteLine(slice.Length); // 3
Console.WriteLine(string.Join(",", slice)); // 2,3,4Open-Ended Ranges
You may omit either bound. ..3 means "from the start up to 3", and 2.. means "from 2 to the end". .. alone copies the whole sequence.
using System;
int[] d = { 10, 20, 30, 40, 50 };
Console.WriteLine(string.Join(",", d[..3])); // 10,20,30
Console.WriteLine(string.Join(",", d[2..])); // 30,40,50
Console.WriteLine(string.Join(",", d[..])); // 10,20,30,40,50Ranges with From-End Indices
Combine .. with ^ to slice relative to the end. ^2.. means "the last two elements".
using System;
int[] d = { 1, 2, 3, 4, 5 };
Console.WriteLine(string.Join(",", d[^2..])); // 4,5
Console.WriteLine(string.Join(",", d[..^1])); // 1,2,3,4 (drop last)Range as a Stored Value
A Range is a value you can name and reuse across different collections of the same length convention.
using System;
Range middle = 1..^1; // drop first and last
int[] a = { 0, 1, 2, 3, 4 };
Console.WriteLine(string.Join(",", a[middle])); // 1,2,3Range.GetOffsetAndLength
To resolve a range against a concrete length, call GetOffsetAndLength(length). It returns the start offset and the slice length, handling from-end indices for you.
using System;
Range r = ^3..^1;
var (offset, length) = r.GetOffsetAndLength(5);
Console.WriteLine(offset + " " + length); // 2 2Range.All
The static Range.All is the same as .. and selects the entire sequence. It is handy when passing ranges as arguments.
using System;
Range whole = Range.All;
int[] d = { 9, 8, 7 };
Console.WriteLine(string.Join(",", d[whole])); // 9,8,7Constructing Range Explicitly
Besides the operator, you can call the constructor or factory methods Range.StartAt and Range.EndAt for clarity.
using System;
Range a = new Range(1, 3);
Range b = Range.StartAt(2);
Range c = Range.EndAt(2);
Console.WriteLine(a + " | " + b + " | " + c);Empty and Invalid Ranges
A range where start equals end produces an empty slice. A range whose start is past its end (after resolving) throws at slice time.
using System;
int[] d = { 1, 2, 3, 4 };
Console.WriteLine(d[2..2].Length); // 0 (empty)
try { var _ = d[3..1]; }
catch (ArgumentOutOfRangeException) { Console.WriteLine("invalid range"); }Putting It Together
Ranges make text and array processing concise. Here we grab a window in the middle of an array without any index arithmetic.
using System;
int[] readings = { 5, 7, 9, 11, 13, 15 };
int[] window = readings[1..^1];
Console.WriteLine(string.Join(",", window)); // 7,9,11,13Quick Check
Make sure you understand the range operator semantics.
Recap
You learned that System.Range describes a slice with the .. operator.
- Start is inclusive, end is exclusive: count = end - start.
- Bounds are optional:
..3,2..,... ..combines with^for from-end slicing.GetOffsetAndLength(length)resolves a range to concrete numbers.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “The Range Type and .. Operator” lesson free?
Yes — the full text of “The Range Type and .. Operator” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.
What will I learn in “The Range Type and .. Operator”?
Take slices with range expressions. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “The Range Type and .. Operator” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Index Type and ^ Operator
- The Range Type and .. Operator
- Ranges with Arrays and Strings
- Custom Types Supporting Ranges