Skip to content
Justice Tech School

Ranges and Indices

^1 is the last item and a..b slices. Works on arrays, strings and spans.

Example
var a = new[] { 10, 20, 30, 40, 50 };
Console.WriteLine(a[^1]);
Console.WriteLine(string.Join(",", a[1..3]));
Console.WriteLine("Hello"[^2..]);

Exercise

Last item.

a[1]

Quiz Test

What is a[1..3]?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.