unitytips: Indices

Unity started to supporting C# 8.0 with version 2020.2 beta and now we can start to use some new features like the Indices.


Indices provide a succinct syntax for accessing single elements in array/collection.

Consider the array below:

  • The 0 index is the same as sequence[0].
  • The ^0 index is the same as sequence[sequence.Length].

Note that sequence[^0] does throw an exception, just as sequence[sequence.Length] does. For any number n, the index ^n is the same as sequence.Length - n.

Loading comments...