新版本的变更 / C# 8.0

using System;

int[] numbers = { 
    // index from start    index from end
    2,    // 0                   ^5
    3,    // 1                   ^4
    5,    // 2                   ^3
    7,    // 3                   ^2
    11    // 4                   ^1
};

//before:
var last = numbers[numbers.Length - 1];
//last is 11
var penultimate = numbers[numbers.Length - 2];
//penultimate is 7

Console.WriteLine("last: " + last);
Console.WriteLine("penultimate: " + penultimate);

//in version 8:
last = numbers[^1];
//last2 is 11
penultimate = numbers[^2];
//penultimate2 is 7

Console.WriteLine("last: " + last);
Console.WriteLine("penultimate: " + penultimate);


The 0 index is the same as sequence[0]. The ^0 index is the same as sequence[sequence.Length]. Note that sequence[^0] does throw an exception, just as sequence[sequence.Length] does. For any number n, the index ^n is the same as sequence.Length - n.