ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: Index from end must be greater than zero; use '^1' to refer to the last element

Last modified: 08 April 2024

The reverse index operator ^ introduced in C# 8.0 might lead to a mistake of referring to the last element with ^0, the same way as you refer to the first element 0. However, the index from end is designed to start with 1, and therefore using ^0 as an index will result in an IndexOutOfRangeException in runtime.

This StackOverflow answer provides a good explanation of why index from end stars with 1 and not with 0.

To fix this problem, replace ^0 with ^1 in the index.