New features keeps getting added to C#. With the release of C# 13 as part of .NET 9, you can now use the implicit index operator, ^
, to initialize a collection in reverse order.
Before C# 13, you had to index elements from the front when using an object initializer. With the introduction of this operator, you get better readability and it can help avoid potential off-by-one errors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var countdown = new List<int> | |
{ | |
[^1] = 0, //Sets the last element to 0 | |
[^2] = 1, | |
[^3] = 2, | |
[^4] = 3, | |
[^5] = 4, | |
[^6] = 5, | |
[^7] = 6, | |
[^8] = 7, | |
[^9] = 8, | |
[^10] = 9 | |
}; |