Ranges And Indices

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.
  1. static string[] weeks = new string[] {  
  2.     "Monday",  
  3.     "Tuesday",  
  4.     "Wednesday",  
  5.     "Thursday",  
  6.     "Friday",  
  7.     "Saturday",  
  8.     "Sunday"  
  9. };  
  10. public static void ExecuteOldRangeIndicesHandling() {  
  11.         Console.WriteLine($ "Third element of an array is: {weeks[2]}");  
  12.         Console.WriteLine($ "Second last element of an array is: {  
  13.                 weeks[weeks.Length - 2]  
  14.             }  
  15.             ");  
  16.             var midWeeks = weeks.ToList().GetRange(2, 3); Console.WriteLine("Elements of midWeeks array are:"); foreach(var week in midWeeks) {  
  17.                 Console.WriteLine(week);  
  18.             }  
  19.             var endofWeeks = weeks.ToList().GetRange(5, 2); Console.WriteLine("Elements of endofWeeks array are:"); foreach(var week in endofWeeks) {  
  20.                 Console.WriteLine(week);  
  21.             }  
  22.         }  
Let's have a look at the modern of C# 8 code syntax to achieve the same as follows.
  1. public static void ExecuteNewRangeIndicesHandling() {  
  2.         Index idx = 2;  
  3.         Console.WriteLine($ "\nThird element of an array is: {  
  4.                     weeks[idx]  
  5.                 }  
  6.                 ");  
  7.                 Console.WriteLine($ "Second last element of an array is: {  
  8.                         weeks[ ^ 2]  
  9.                     }  
  10.                     ");  
  11.                     Range range = 2. .5; //Start from 2nd index and goes before 5th index means index 2, 3 and 4  
  12.                     var midWeeks = weeks[range]; Console.WriteLine("Elements of midWeeks array are:"); foreach(var week in midWeeks) {  
  13.                         Console.WriteLine(week);  
  14.                     }  
  15.                     Console.WriteLine("Elements of endofWeeks array are:");  
  16.                     var endofWeeks = weeks[ ^ 2.. ^ 0]; foreach(var week in endofWeeks) {  
  17.                         Console.WriteLine(week);  
  18.                     }  
  19.                 }  
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.
  1. public static void ExecuteUnboundedRange() {  
  2.     var midWeeks = weeks[..3]; // Start from 0th and goes before 3rd index means index 0, 1 and 2  
  3.     Console.WriteLine("First three elements of midWeeks array are:");  
  4.     foreach(var week in midWeeks) {  
  5.         Console.WriteLine(week);  
  6.     }  
  7.     Console.WriteLine("last two elements of endofWeeks array are:");  
  8.     var endofWeeks = weeks[ ^ 2..];  
  9.     foreach(var week in endofWeeks) {  
  10.         Console.WriteLine(week);  
  11.     }  
  12. }  

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.
Author
Prakash Tripathi
29 40.3k 6.3m
Next » Nullable Reference Types