Take Method with Ranges
Learn how to select a slice of elements of a collection.
We'll cover the following
What are indices and ranges?
Starting from C# version 8.0, we have two new operators: the index from the end operator (^
) and the range operator (..
).
With the index from the end operator (^
), we can access elements from the end of arrays—for example, the last element of an array would be myArray[^1]
. Before, we had to use the length of the array to access elements from the end with myArray[myArray.Length - 1]
.
On the other hand, with the range operator (..
) we can access a slice, or range, of elements from an array. The range operator uses a starting position and an ending position. The output doesn’t include the element at the ending position of the range.
Let’s select a range of movies from our catalog.
Get hands-on with 1400+ tech skills courses.