C# 8.0 New Feature - Ranges And Indices

In this article, we are going to learn the new features of C# 8.0, Ranges and Indices. This introduces new operators and new types - System.Index and System.Range.

Introduction

 
In this article, we are going to learn the new features of C# 8.0, Ranges and Indices. This introduces new operators and new types - System.Index and System.Range.
 

System.Range

 
In C#, there is no way to perform the 'slice' or 'ranges' for collections. There is no way to skip and take data without using LINQ methods. So, there's a new way, i.e., using the new range operator 'x..y'. We call this '..' operator the 'Range Operator'. The above example 'x' is nothing but starting index and 'y' is the ending index. These are also called as inclusive and exclusive.
 
An example of using LINQ to get the element 2nd, 3rd, and 4th,
  1. string[] techArray = { "C""C++""C#""F#""JavaScript""Angular""TypeScript""React""GraphQL" };  
  2.   
  3. var techSlice = techArray.Skip(1).Take(3).ToArray();  
  4.   
  5. foreach (var item in techSlice)  
  6. {  
  7.     Console.WriteLine(item);  //C++ C# F#
  8. }  
An example of using 'range operator'(x..y) to get the element 2nd, 3rd, and 4th:
  1. string[] techArray = { "C""C++""C#""F#""JavaScript""Angular""TypeScript""React""GraphQL" };  
  2.   
  3. foreach (var item in techArray[1..4])  
  4. {  
  5.     Console.WriteLine(item);  //C++ C# F#
  6. }  
An example of defining this 'x..y' operator with 'Range' type as follows,
  1. string[] techArray = { "C""C++""C#""F#""JavaScript""Angular""TypeScript""React""GraphQL" };  
  2.   
  3. Range range = 1..4;  
  4.   
  5. foreach (var item in techArray[range])  
  6. {  
  7.     Console.WriteLine(item);  //C++ C# F#
  8. }  

System.Index

 
This is the second new type introduced in C# 8.0. We have seen a couple of examples about 'Range'. Here, in the 'x..y'(range operator) - x and y are the types of 'Index'. Range internally uses the 'Index' type.
 
Let's see how to use Index type with the range operator.
  1. string[] techArray = { "C""C++""C#""F#""JavaScript""Angular""TypeScript""React""GraphQL" };  
  2. Index startIndex = 1;  
  3. Index endIndex = 4;  
  4.   
  5. foreach (var item in techArray[startIndex..endIndex])  
  6. {  
  7.     Console.WriteLine(item);  //C++ C# F#
  8. }  

The Hat(^) operator

 
This is the second new operator introduced in C# 8.0, named as the 'Hat'(^) operator. What we can do with this operator? Example - get the last element from the array currently, we are doing like as follows,
  1. string[] techArray = { "C""C++""C#""F#""JavaScript""Angular""TypeScript""React""GraphQL" };  
  2. var lastOldWay = techArray[techArray.Length - 1];  
  3.   
  4. Console.WriteLine("Old way : " + lastOldWay + "(techArray[techArray.Length - 1])");  //GraphQL
Using the hat(^) operator,
  1. string[] techArray = { "C""C++""C#""F#""JavaScript""Angular""TypeScript""React""GraphQL" };  
  2. var lastNewWay = techArray[^1];  
  3.   
  4. Console.WriteLine("New way : " + lastNewWay + "(techArray[^1])");  //GraphQL
Using Ranges for strings to get the substring,
  1. var welcome = "Welcome to C# Corner!";  
  2.   
  3. Console.WriteLine(welcome[^10..]);  //C# Corner!  
Following are some more examples of using new types and operators,
 
Example 1 - Use Ranges and Index in array
  1.  var people = new Person[] {  
  2.     new Person("Mangesg""G"),  
  3.     new Person("Jeetendra""G"),  
  4.     new Person("Avinash""J"),  
  5.     new Person("Sai""B"),  
  6. };  
  7.   
  8. foreach (var per in people[1..^2])  
  9. {  
  10.     Console.WriteLine(per.FirstName + ' ' + per.LastName); //Jeetendra G  
  11. }   
Example 2 - Exclude first element
  1. string[] techArray = { "C""C++""C#""F#""JavaScript""Angular""TypeScript""React""GraphQL" };  
  2.   
  3. foreach (var item in techArray[1..])  
  4. {  
  5.     Console.WriteLine(item);  
  6. }  
Example 3 - Get first 3 elements
  1. string[] techArray = { "C""C++""C#""F#""JavaScript""Angular""TypeScript""React""GraphQL" };  
  2.   
  3. foreach (var item in techArray[..3])  
  4. {  
  5.     Console.WriteLine(item);  //C, C++, C#
  6. }  
Example 4 - Exclude first and last element
  1. string[] techArray = { "C""C++""C#""F#""JavaScript""Angular""TypeScript""React""GraphQL" };  
  2.   
  3. foreach (var item in techArray[1..^1])  
  4. {  
  5.     Console.WriteLine(item);  
  6. }  
Example 5 - Getting subsrting from the string direcctly
  1. Console.WriteLine("Jeetendra"[..4]);  //Jeet
You can also download all this example from here.
 

Summary

In this article, we discussed what Ranges and Indices in C# 8.0 are and how to use this with examples. If you have any suggestions or queries regarding this article, please contact me.

Stay tuned for other concepts of C# 8.0.

“Learn It, Share it.”