C#

C#

Made by DeepSource

Consider simplifying the element access CS-R1019

Anti-pattern
Major
Autofix

Individual elements in a string or an array can be accessed via the bracketed expression. The norm for accessing elements from last is usually in the format of foo[ub - i] where ub is the upper bound and i is an index. This expression however can be simplified and rewritten as foo[^i].

Bad Practice

var lastElement = array[array.Length - 1];

Recommended

var lastElement = array[^1];