>
>
>
V3199. The index from end operator is u…


V3199. The index from end operator is used with the value that is less than or equal to zero. Collection index will be out of bounds.

The analyzer has detected that a collection element is being accessed using the '^' operator with a value less than or equal to 0. This results in an exception of the 'IndexOutOfRangeException' type.

Let's look at the following example:

T GetLastItem<T>(T[] array)
{
    return array[^0];
}

The '^' operator indicates that the index is counted from the end of the sequence. It may not be obvious that '^0' is equal to 'array.Length'. Attempting to get the last collection element via '^0' results in the exception, just like when using 'array[array.Length]'.

Here's the fixed code:

T GetLastItem<T>(T[] array)
{
    return array[^1]; 
}

This diagnostic is classified as: