Range Indexer
11/20/2024
With C# 12's collection expression feature, the range indexers has become also more powerful.
These two constructs along with the C#'s spreads feature allow to easily create new collections by slicing and concatenating existing ones. For example, given the following snippet:
int[] a = [1, 2]; int[] b = [3, 4, 5]; int[] c = [..a, ..b[..1]];
When executing the snippet, variable c
will reference an integer array containing 1 2 and 3 values. While these operations are universal in many languages for a longer time, they are only introduced as new in C# 12.