Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# Int 64 Struct
Next article icon

Range Structure in C# 8.0

Last Updated : 06 Feb, 2025
Comments
Improve
Suggest changes
Like Article
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


Next Article
C# Int 64 Struct
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-8.0

Similar Reads

  • Index Struct in C# 8.0
    Index Struct introduced in C# 8.0. It is used to represent a type that can be used as an index of a collection or sequence that starts from either start or end. It provides a new index style to access elements using the ^ (caret) operator. It is used to find the last elements of the specified collec
    4 min read
  • C# Data Structures
    Data Structures are an important part of programming language. When we are creating solutions for real-world problems, choosing the right data structure is critical because it can impact the performance of algorithms. If we know the fundamentals of data structures and know how to use them effectivel
    15+ min read
  • C# Int 64 Struct
    In C#, the Int64 struct represents a 64-bit signed integer and is commonly used as the long data type. It belongs to the System namespace and provides various methods to perform operations like mathematical computations, parsing, and type conversion. The Int64 struct inherits from ValueType, which i
    3 min read
  • C# Int16 Struct
    In C#, Int16 Struct defined under the System namespace represents a 16-bit signed integer also known as a short datatype etc. We can also call the methods of math class to perform mathematical operations. Int16 struct inherits the ValueType class which inherits the Object class. Characteristics are
    3 min read
  • C# Int32 Struct
    In C#, the Int32 struct represents a 32-bit signed integer and is commonly used as the int data type. It belongs to the System namespace and provides various methods to perform operations like mathematical computations, parsing, and type conversion. The Int32 struct inherits from ValueType, which in
    3 min read
  • Range and Indices in C# 8.0
    Range and Indices introduced in C# 8.0. released as part of .NET Core 3.0 and .NET Framework 4.8 they provide a short syntax to represent or access a single or a range of elements from the given sequence or collections. System.Index: It represents an index in the given sequence or collection.System.
    4 min read
  • C# - Char Struct
    In C#, the Char struct is used to represent a single Unicode character as a UTF-16 code unit, defined under the System namespace. A Char in C# is a 16-bit value, and it can represent characters in the Basic Multilingual Plane (BMP). Characters beyond 0xFFFF are represented by surrogate pairs (two Ch
    4 min read
  • C# | SByte Struct Fields
    In C#, Sbyte Struct comes under the System namespace which represents an 8-bit signed integer. The SByte value type represents integers with values ranging from -128 to +127. There are the two fields in the System.SByte Struct as follows: SByte.MaxValue Field SByte.MinValue Field SByte.MaxValue Fiel
    2 min read
  • Decimal.Subtract() Method in C#
    This method is used to subtract the one specified Decimal value from another. Syntax: public static decimal Subtract (decimal a1, decimal a2); Parameters: a1: This parameter specifies the minuend. a2: This parameter specifies the subtrahend. Return Value: Result of subtracting a2 from a1. Exceptions
    2 min read
  • C# Byte Struct
    In C#, Byte Struct is used to represent 8-bit unsigned integers. The Byte is an immutable value type and the range of Byte is from 0 to 255. This class allows us to create Byte data types and we can perform mathematical and bitwise operations on them like addition, subtraction, multiplication, divis
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences