C# Feature - Index operator
Overview
Section titled “Overview”The index operator ^ addresses elements from the end of an array, span, or indexer-enabled type.
Introduced In
Section titled “Introduced In”C# 8.0 (2019)
Before
Section titled “Before”int last = values[values.Length - 1];int last = values[^1];Annotated Example
Section titled “Annotated Example”char lastLetter = word[^1];char secondLastLetter = word[^2];Gotchas & Best Practices
Section titled “Gotchas & Best Practices”^1means the last element, not one past the end.- Use it when indexing from the end improves readability over manual arithmetic.
Related Features
Section titled “Related Features”Version Notes
Section titled “Version Notes”- C# 8 introduced
Indexand the^operator for end-based indexing.