In this article, we will go through the Ranges and indices feature of C# 8.0.
Range and Indices are the great additions in the C# world. Due to these constructs, handling indexes have become fairly easy. Below is a summary of the changes in this feature.
- Index represents an index in an array or sequence.
- The ^ operator specifies the relative index from the end of an array.
- Range represents a subrange of an array.
- The Range operator .. specifies the start (Inclusive) and end (exclusive) of a range.
The following table states the comparison of old and new syntax.
Task | Old-style (Before C# 8) | New style (in C# 8) |
Getting the second element from an array | array[2] | array[2] or Index idx = 2; array[idx] |
Getting third the last element of an array | array[array.Length-3] | array[^3] |
Get the sub-range of an array from element index 2 till 4 | array.ToList().GetRange(2, 3); | array[2..5] or Range range= 2..5; array[range] |
Get the sub-range of an array from second last element till last element | array.ToList(). GetRange(weeks.Length- 2, 2); | array[^2..^0]; or array[^2..]; or Range range= ^2..^0; array[range] |
Get the first three elements of an array. | array.ToList().GetRange(0, 3); | array[..3]; |
Now, let's take an example to understand this more and start with traditional style as follows.
- static string[] weeks = new string[] {
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday",
- "Sunday"
- };
- public static void ExecuteOldRangeIndicesHandling() {
- Console.WriteLine($ "Third element of an array is: {weeks[2]}");
- Console.WriteLine($ "Second last element of an array is: {
- weeks[weeks.Length - 2]
- }
- ");
- var midWeeks = weeks.ToList().GetRange(2, 3); Console.WriteLine("Elements of midWeeks array are:"); foreach(var week in midWeeks) {
- Console.WriteLine(week);
- }
- var endofWeeks = weeks.ToList().GetRange(5, 2); Console.WriteLine("Elements of endofWeeks array are:"); foreach(var week in endofWeeks) {
- Console.WriteLine(week);
- }
- }
Let's have a look at the modern of C# 8 code syntax to achieve the same as follows.
- public static void ExecuteNewRangeIndicesHandling() {
- Index idx = 2;
- Console.WriteLine($ "\nThird element of an array is: {
- weeks[idx]
- }
- ");
- Console.WriteLine($ "Second last element of an array is: {
- weeks[ ^ 2]
- }
- ");
- Range range = 2. .5;
- var midWeeks = weeks[range]; Console.WriteLine("Elements of midWeeks array are:"); foreach(var week in midWeeks) {
- Console.WriteLine(week);
- }
- Console.WriteLine("Elements of endofWeeks array are:");
- var endofWeeks = weeks[ ^ 2.. ^ 0]; foreach(var week in endofWeeks) {
- Console.WriteLine(week);
- }
- }
Finally, let's go through the improvement in terms of unbounded arrays. With C# 8, you can just give one end to get subrange in an array as follows.
- public static void ExecuteUnboundedRange() {
- var midWeeks = weeks[..3];
- Console.WriteLine("First three elements of midWeeks array are:");
- foreach(var week in midWeeks) {
- Console.WriteLine(week);
- }
- Console.WriteLine("last two elements of endofWeeks array are:");
- var endofWeeks = weeks[ ^ 2..];
- foreach(var week in endofWeeks) {
- Console.WriteLine(week);
- }
- }
Conclusion
You can clearly see that C# 8 syntax is more crisp and easy to use. The new syntax is clearer to know what subrange is being used. This can be pretty useful when performing analysis on the subrange of an array or sequence. One of the scenarios of using the Indices and Ranges is calculating moving average in a loop.