skip to content
Khalid Abuhakmeh logo Khalid Abuhakmeh

The ^ index operator

C# 8

Code card: The ^ index operator. Program output: 1 / 9 / 8

^n counts from the end: ^1 is the last item. Skip numbers[numbers.Length - 1]. It is available since C# 8. The key line is var last = numbers[^1] in index.cs. It fits in 9 lines; run it with dotnet run index.cs.

Did this tip save you some time? Consider buying me a coffee to fuel the next one.

Buy me a coffee
Railway Referral

Need somewhere to deploy?

My link gets you $20 in credits.

Claim $20 credit

I may earn a commission if you become a customer.

index.cs
int[] numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var first = numbers[0];
var last = numbers[^1];
var second = numbers[^2];
Console.WriteLine(first);
Console.WriteLine(last);
Console.WriteLine(second);
Output
$ dotnet run index.cs
1
9
8