Range and Indices in C# 8.0

28 Nov, 2019
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(^)using System;  namespace example {  class GFG {      // Main Method    static void Main(string[] args)    {        // Creating and initializing an array        int[] myarr = new int[] {34, 56, 77, 88, 90, 45};          // Simple getting the index value        Console.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        // index        Console.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]);    }}}Output: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(^)using System;  namespace example {  class GFG {      // Main Method    static void Main(string[] args)    {        // Creating and initializing an array        int[] myarr = new int[] {34, 56,                        77, 88, 90, 45};          // Simply getting the index value        Console.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        // index        Console.WriteLine("The end values of the specified index:");        Console.WriteLine(myarr[^0]);    }}}Output:Values of the specified index:34The 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 22You 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 variableusing System;   namespace example {   class GFG {       // Main Method    static void Main(string[] args)    {           // Creating and initializing an array        int[] number = new int[] {1, 2, 3, 4,                              5, 6, 7, 8, 9};           // Declare an index        // as a variable        Index i = ^6;        var val = number[i];           // Displaying number        Console.WriteLine("Number: " + val);    }}}Output:Number: 4
// 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(^)using System;  namespace example {  class GFG {      // Main Method    static void Main(string[] args)    {        // Creating and initializing an array        int[] myarr = new int[] {34, 56, 77, 88, 90, 45};          // Simple getting the index value        Console.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        // index        Console.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]);    }}}
Output:
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(^)using System;  namespace example {  class GFG {      // Main Method    static void Main(string[] args)    {        // Creating and initializing an array        int[] myarr = new int[] {34, 56,                        77, 88, 90, 45};          // Simply getting the index value        Console.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        // index        Console.WriteLine("The end values of the specified index:");        Console.WriteLine(myarr[^0]);    }}}Output:Values of the specified index:34The 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
Example:
// C# program to illustrate the use // of the index from end operator(^)using System;  namespace example {  class GFG {      // Main Method    static void Main(string[] args)    {        // Creating and initializing an array        int[] myarr = new int[] {34, 56,                        77, 88, 90, 45};          // Simply getting the index value        Console.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        // index        Console.WriteLine("The end values of the specified index:");        Console.WriteLine(myarr[^0]);    }}}
Output:
Values of the specified index:34The 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 variableusing System;   namespace example {   class GFG {       // Main Method    static void Main(string[] args)    {           // Creating and initializing an array        int[] number = new int[] {1, 2, 3, 4,                              5, 6, 7, 8, 9};           // Declare an index        // as a variable        Index i = ^6;        var val = number[i];           // Displaying number        Console.WriteLine("Number: " + val);    }}}Output:Number: 4
Example:
// C# program to illustrate how// to declare a index as a variableusing System;   namespace example {   class GFG {       // Main Method    static void Main(string[] args)    {           // Creating and initializing an array        int[] number = new int[] {1, 2, 3, 4,                              5, 6, 7, 8, 9};           // Declare an index        // as a variable        Index i = ^6;        var val = number[i];           // Displaying number        Console.WriteLine("Number: " + val);    }}}
Output:
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(..)using System;   namespace example {   class GFG {       // Main Method    static void Main(string[] args)    {        // Creating and initializing an array        string[] myemp = new string[] {"Anu", "Priya", "Rohit",                     "Amit", "Shreya", "Rinu", "Sumit", "Zoya"};           Console.Write("Name of the employees in project A: ");        var P_A = myemp[0..3];        foreach(var emp1 in P_A)            Console.Write($" [{emp1}]");           Console.Write("\nName of the employees in project B: ");        var P_B = myemp[3..5];        foreach(var emp2 in P_B)            Console.Write($" [{emp2}]");           Console.Write("\nName of the employees in project C: ");            var P_C = myemp[1..^2];            foreach (var emp3 in P_C)                Console.Write($" [{emp3}]");           Console.Write("\nName of the employees in project D: ");        var P_D = myemp[..];        foreach(var emp4 in P_D)            Console.Write($" [{emp4}]");           Console.Write("\nName of the employees in project E: ");        var P_E = myemp[..2];        foreach(var emp5 in P_E)            Console.Write($" [{emp5}]");           Console.Write("\nName of the employees in project F: ");        var P_F = myemp[6..];        foreach(var emp6 in P_F)            Console.Write($" [{emp6}]");           Console.Write("\nName of the employees in project G: ");        var P_G = myemp[^3.. ^ 1];        foreach(var emp7 in P_G)            Console.Write($" [{emp7}]");    }}}Output: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 variableusing System;   namespace example {   class GFG {       // Main Method    static void Main(string[] args)    {           // Creating and initializing an array        int[] number = new int[] {1, 2, 3, 4,                              5, 6, 7, 8, 9};           Console.Write("Number: ");           // Declaring a range as a variable        Range num = 1..3;        int[] val = number[num];           // Displaying number        foreach(var n in val)            Console.Write($" [{n}]");    }}}Output:Number:  [2] [3]

// 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(..)using System;   namespace example {   class GFG {       // Main Method    static void Main(string[] args)    {        // Creating and initializing an array        string[] myemp = new string[] {"Anu", "Priya", "Rohit",                     "Amit", "Shreya", "Rinu", "Sumit", "Zoya"};           Console.Write("Name of the employees in project A: ");        var P_A = myemp[0..3];        foreach(var emp1 in P_A)            Console.Write($" [{emp1}]");           Console.Write("\nName of the employees in project B: ");        var P_B = myemp[3..5];        foreach(var emp2 in P_B)            Console.Write($" [{emp2}]");           Console.Write("\nName of the employees in project C: ");            var P_C = myemp[1..^2];            foreach (var emp3 in P_C)                Console.Write($" [{emp3}]");           Console.Write("\nName of the employees in project D: ");        var P_D = myemp[..];        foreach(var emp4 in P_D)            Console.Write($" [{emp4}]");           Console.Write("\nName of the employees in project E: ");        var P_E = myemp[..2];        foreach(var emp5 in P_E)            Console.Write($" [{emp5}]");           Console.Write("\nName of the employees in project F: ");        var P_F = myemp[6..];        foreach(var emp6 in P_F)            Console.Write($" [{emp6}]");           Console.Write("\nName of the employees in project G: ");        var P_G = myemp[^3.. ^ 1];        foreach(var emp7 in P_G)            Console.Write($" [{emp7}]");    }}}
Output:
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 variableusing System;   namespace example {   class GFG {       // Main Method    static void Main(string[] args)    {           // Creating and initializing an array        int[] number = new int[] {1, 2, 3, 4,                              5, 6, 7, 8, 9};           Console.Write("Number: ");           // Declaring a range as a variable        Range num = 1..3;        int[] val = number[num];           // Displaying number        foreach(var n in val)            Console.Write($" [{n}]");    }}}Output:Number:  [2] [3]

Example:
// C# program to illustrate how to// declare a range as a variableusing System;   namespace example {   class GFG {       // Main Method    static void Main(string[] args)    {           // Creating and initializing an array        int[] number = new int[] {1, 2, 3, 4,                              5, 6, 7, 8, 9};           Console.Write("Number: ");           // Declaring a range as a variable        Range num = 1..3;        int[] val = number[num];           // Displaying number        foreach(var n in val)            Console.Write($" [{n}]");    }}}
Output:
Number:  [2] [3]

CSharp-8.0
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
Introduction to .NET Framework
C# | Delegates
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework
C# | Method Overriding
C# | Constructors
C# | String.IndexOf( ) Method | Set - 1
C# | Class and Object
Extension Method in C#