33 var arr = new[] {1, 2, 3, 4, 5};
34 // CS-R1019: `arr[arr.Length - 1]` can be rewritten as `arr[^1].`
35 // https://deepsource.io/directory/analyzers/csharp/issues/CS-R1019
36 var last = arr[arr.Length - 1]; 37
38 var emptyString = string.Empty;
39 // CS-R1014: Use `string.IsNullOrEmpty` or `string.IsNullOrWhiteSpace` to check for empty strings.
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]
.
var lastElement = array[array.Length - 1];
var lastElement = array[^1];