Open In App

Range Structure in C# 8.0

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Range struct predefined structure introduced in C# 8.0. This is used to represent a range that has start and end indexes. It provides a new style to create a range using the (..) operator. This operator is used to create a range that has a starting and ending index. It also allows the creation of a variable of the range type. It makes it easier to make slices of collections such as arrays, strings, and spans.

Types of Range:

  • Bounded Range: Specifies both start and end indices (e.g., 1..6)
  • Unbounded Range: Specifies only start or end indices (e.g., ..4, 2.., ..)

Example:

C#
// C# program to show how to create ranges using System; class Geeks { 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 initialize the range  // variables 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:

RangeOutput

Constructor

Range(Index, Index): This constructor is used to create a new Range instance with the specified starting and ending indexes.

Example:

C#
// C# Program to demonstrate Range Constructor class Geeks {  public static void Main()  {  // Array  var arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };  // Range Created using the Range class constructor  var r = new Range(2, 5);  Console.WriteLine(String.Join(", ", arr[r]));   } } 

Output:

RangeClassConstructor

Properties

  • 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

Example:

C#
// C# Program to demonstrate Properties using System; class Geeks {  public static void Main()  {  // Array  int[] arr = { 1, 2, 3, 4, 5 };  // Create a range from the first element to the last  Range range = 0..arr.Length;  // Using Start and End properties  Console.WriteLine("Start Index: " + range.Start.Value);   Console.WriteLine("End Index: " + range.End.Value);   // Display the range elements  Console.WriteLine("Range Elements: "  + string.Join(", ", arr[range]));  } } 

Output:

RangePropertyExample

Methods

Method

Description

EndAt(Index)

Used to create a Range object starting from the first element in the collection to a specified end index.

Equals()

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)

Used to calculate the start offset and length of the given range object with the help of a collection length

StartAt(Index)

Used to create a new Range instance starting from a specified start index to the end of the collection.

ToString()

Returns the string representation of the current Range object.

Example:

C#
// C# Program to demonstrate Methods of Range Struct class Geeks {  public static void Main()  {  // Array  var arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };  // Create a range from the first element to the last  Range r = 0..arr.Length;    // Create a range starting at index 2  Range r2 = Range.StartAt(2);    // Create a range ending at index 3  Range r3 = Range.EndAt(3);   // Display the Range object  Console.WriteLine("Range of r: " + r.ToString());  // Display the elements of range   Console.WriteLine("Elements of r: "+ String.Join(" ", arr[r]));  Console.WriteLine("Elements of r2: "+ String.Join(" ", arr[r2]));  Console.WriteLine("Elements of r3: "+ String.Join(" ", arr[r3]));  // Checking equality of ranges  Console.WriteLine("r3 is equal to r2: "+  r3.Equals(r2));   Console.WriteLine("r3 is equal to r: "+   r3.Equals(r));    } } 

Output:

RangeMethodExample

Article Tags :

Explore