Range Structure in C# 8.0
C# 8.0 introduced a new predefined structure that is known as Range struct. This struct is used to represent a range that has a start and end indexes. It provides a new style to create a range using .. operator. This operator is used to create a range that has a starting and ending index. Also with the help of the Range struct you are allowed to create a variable of the range type.
// C# program to illustrate how to create ranges using System; namespace range_example { class GFG { // Main Method static void Main(string[] args) { int[] marks = new int[] {23, 45, 67, 88, 99, 56, 27, 67, 89, 90, 39}; // Creating variables of range type // And initalize the range // varibales with a range // Using .. operator Range r1 = 1..5; Range r2 = 6..8; var a1 = marks[r1]; Console.Write("Marks List 1: "); foreach(var st_1 in a1) Console.Write($" {st_1} "); var a2 = marks[r2]; Console.Write("\nMarks List 2: "); foreach(var st_2 in a2) Console.Write($" {st_2} "); // Creating a range // Using .. operator var a3 = marks[2..4]; Console.Write("\nMarks List 3: "); foreach(var st_3 in a3) Console.Write($" {st_3} "); var a4 = marks[4..7]; Console.Write("\nMarks List 4: "); foreach(var st_4 in a4) Console.Write($" {st_4} "); } } } |
Output:
Marks List 1: 45 67 88 99 Marks List 2: 27 67 Marks List 3: 67 88 Marks List 4: 99 56 27
Constructor
| Constructor | Description |
|---|---|
| Range(Index, Index) | This constructor is used to create a new Range instance with the specified starting and ending indexes. |
Properties
| Property | Description |
|---|---|
| All | This property is used to get a Range object that starts from the first element to the end. |
| End | This property is used to get an Index that represents the exclusive end index of the range. |
| Start | This property is used to get the inclusive start index of the Range. |
Methods
| Method | Description |
|---|---|
| EndAt(Index) | This method is used to create a Range object starting from the first element in the collection to a specified end index. |
| Equals() | This method is used to check whether the current range is equal to a specified range. |
| GetHashCode() | This method is used to find the hash code for this instance. |
| GetOffsetAndLength(Int32) | This method is used to calculate the start offset and length of the given range object with the help of a collection length. |
| StartAt(Index) | This method is used to create a new Range instance starting from a specified start index to the end of the collection. |
| ToString() | This method returns the string representation of the current Range object. |
Recommended Posts:
- Difference between Class and Structure in C#
- How to access structure elements using Pointers in C#
- Range Constructor in C#
- Range and Indices in C# 8.0
- How to Create a Range to a Specified End in C#?
- Finding the End Index of the Specified Range in C#
- How to Create a Range From a Specified Start in C#?
- Getting the Hash Code of the Specified Range in C#
- C# | Remove a range of elements from the ArrayList
- C# | Getting an enumerator for a range of elements in the ArrayList
- C# | Removing a range of elements from the List
- Finding all the Elements of a Range from Start to End in C#
- Finding the Start Index of the Specified Range in C#
- C# | Reverse the order of the elements in the entire List or in the specified range
- C# | Reverse the order of the elements in the entire ArrayList or in the specified range
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.


