As we already know about the Range and Indices. We use them several times in our programs, they provide a short syntax to represent or access a single or a range of elements from the given sequence or collections. In this article, we will learn what’s newly added in the range and indices in C# 8.0. In C# 8.0, the following new things are added in the range and indices:
1. Two New Types:
- System.Range: It represents a sub-range of the given sequence or collection.
- System.Index: It represents an index into the given sequence or collection.
2. Two New Operators:
- ^ Operator: It is known as the index from the end operator. It returns an index that is relative to the end of the sequence or collection. It is the most compact and easiest way to find the end elements compare to earlier methods.
// Old Method var lastval = myarr[myarr.Length-1] // New Method var lastval = myarr[^1]
Example:
// C# program to illustrate the use// of the index from end operator(^)usingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Creating and initializing an arrayint[] myarr =newint[] {34, 56, 77, 88, 90, 45};// Simple getting the index valueConsole.WriteLine("Values of the specified indexes:");Console.WriteLine(myarr[0]);Console.WriteLine(myarr[1]);Console.WriteLine(myarr[2]);Console.WriteLine(myarr[3]);Console.WriteLine(myarr[4]);Console.WriteLine(myarr[5]);// Now we use index from end(^)// operator with the given index// This will return the end value// which is related to the specified// indexConsole.WriteLine("The end values of the specified indexes:");Console.WriteLine(myarr[^1]);Console.WriteLine(myarr[^2]);Console.WriteLine(myarr[^3]);Console.WriteLine(myarr[^4]);Console.WriteLine(myarr[^5]);Console.WriteLine(myarr[^6]);}}}chevron_rightfilter_noneOutput:
Values of the specified indexes: 34 56 77 88 90 45 The end values of the specified indexes: 45 90 88 77 56 34
Explanation: In the above example, we have an array of int type named myarr. Here, first we simply get the values of the specified index that is:
34, 56, 77, 88, 90, 45 Index : Value [0] : 34 [1] : 56 [2] : 77 [3] : 88 [4] : 90 [5] : 45
Now we find the last value of the specified index with the help of ^ operator that is:
Index : Value [^1] : 45 [^2] : 90 [^3] : 88 [^4] : 77 [^5] : 56 [^6] : 34
Important Points:
- The working of this operator is similar to myarr[arr.Length].
- You are not allowed to use myarr[^0] if you use this, then this will throw an error because the end index starts from ^1, not from ^0 as shown in the below example:
Example:
// C# program to illustrate the use// of the index from end operator(^)usingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Creating and initializing an arrayint[] myarr =newint[] {34, 56,77, 88, 90, 45};// Simply getting the index valueConsole.WriteLine("Values of the specified index:");Console.WriteLine(myarr[0]);// Now we use index from end(^)// operator with the given index// This will return the end value// which is related to the specified// indexConsole.WriteLine("The end values of the specified index:");Console.WriteLine(myarr[^0]);}}}chevron_rightfilter_noneOutput:
Values of the specified index:
34
The end values of the specified index:
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
at example.Program.Main(String[] args) in /Users/anki/Projects/example/example/Program.cs:line 22 - You are allowed to use the index as a variable and this variable place in between brackets[]. As shown in the below example:
Example:
// C# program to illustrate how// to declare a index as a variableusingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Creating and initializing an arrayint[] number =newint[] {1, 2, 3, 4,5, 6, 7, 8, 9};// Declare an index// as a variableIndex i = ^6;varval = number[i];// Displaying numberConsole.WriteLine("Number: "+ val);}}}chevron_rightfilter_noneOutput:
Number: 4
- .. Operator: It is known as the range operator. And it specifies the start and end as its operands of the given range. It is the most compact and easiest way to find the range of the elements from the specified sequence or collection in comparison to earlier methods.
// Old Method var arr = myemp.GetRange(1, 5); // New Method var arr = myemp[2..3]
Example:
// C# program to illustrate the// use of the range operator(..)usingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Creating and initializing an arraystring[] myemp =newstring[] {"Anu","Priya","Rohit","Amit","Shreya","Rinu","Sumit","Zoya"};Console.Write("Name of the employees in project A: ");varP_A = myemp[0..3];foreach(varemp1inP_A)Console.Write($" [{emp1}]");Console.Write("\nName of the employees in project B: ");varP_B = myemp[3..5];foreach(varemp2inP_B)Console.Write($" [{emp2}]");Console.Write("\nName of the employees in project C: ");varP_C = myemp[1..^2];foreach(varemp3inP_C)Console.Write($" [{emp3}]");Console.Write("\nName of the employees in project D: ");varP_D = myemp[..];foreach(varemp4inP_D)Console.Write($" [{emp4}]");Console.Write("\nName of the employees in project E: ");varP_E = myemp[..2];foreach(varemp5inP_E)Console.Write($" [{emp5}]");Console.Write("\nName of the employees in project F: ");varP_F = myemp[6..];foreach(varemp6inP_F)Console.Write($" [{emp6}]");Console.Write("\nName of the employees in project G: ");varP_G = myemp[^3.. ^ 1];foreach(varemp7inP_G)Console.Write($" [{emp7}]");}}}chevron_rightfilter_noneOutput:
Name of the employees in project A: [Anu] [Priya] [Rohit] Name of the employees in project B: [Amit] [Shreya] Name of the employees in project C: [Priya] [Rohit] [Amit] [Shreya] [Rinu] Name of the employees in project D: [Anu] [Priya] [Rohit] [Amit] [Shreya] [Rinu] [Sumit] [Zoya] Name of the employees in project E: [Anu] [Priya] Name of the employees in project F: [Sumit] [Zoya] Name of the employees in project G: [Rinu] [Sumit]
Important Points:
- When you create a range using range operator, then it will not add the last element. For example, we have an array {1, 2, 3, 4, 5, 6 }, now we want to print range[1..3], then it will print 2, 3. It does not print 2, 3, 4.
- In Range, if a range contains starting and ending index like Range[start, end], then such types of ranges are known as the bounded range.
- In Range, if a range contains only starting, ending index or doesn’t contain starting and ending indexes like Range[start..], or Range[..end], or Range[..], then such types of ranges are known as the unbounded range.
- You are allowed to use range as a variable and this variable place in between brackets[]. As shown in the below example:
Example:
// C# program to illustrate how to// declare a range as a variableusingSystem;namespaceexample {classGFG {// Main MethodstaticvoidMain(string[] args){// Creating and initializing an arrayint[] number =newint[] {1, 2, 3, 4,5, 6, 7, 8, 9};Console.Write("Number: ");// Declaring a range as a variableRange num = 1..3;int[] val = number[num];// Displaying numberforeach(varninval)Console.Write($" [{n}]");}}}chevron_rightfilter_noneOutput:
Number: [2] [3]
Recommended Posts:
- Range Constructor in C#
- Getting the Hash Code of the Specified Range in C#
- Finding all the Elements of a Range from Start to End in C#
- Finding the End Index of the Specified Range in C#
- Finding the Start Index of the Specified Range in C#
- How to Create a Range From a Specified Start in C#?
- How to Create a Range to a Specified End in C#?
- Range Structure in C# 8.0
- Interpolated Verbatim Strings in C# 8.0
- Null-Coalescing Assignment Operator in C# 8.0
- Switch Expression in C# 8.0
- Default Interface Methods in C# 8.0
- Check if the given ranges are equal or not in C#
- How to Create a Range From a Specified Start in C#?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

