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# | Operator Overloading
Next article icon

Range Constructor in C#

Last Updated : 28 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Range(Index, Index) Constructor is the part of Range Struct. This constructor is used to create a new instance of Range along with the specified starting and ending indexes. When you create a range using the range operator or constructor, then it will not add the last element or end index 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.
Syntax: 
 

public Range(Index start, Index end);

Here, the start represents the starting index of the range and the end represents the last index of the range.
Example 1:
 

CSharp




// C# program to illustrate how to
// use Range(Index, Index) constructor
using System;
  
namespace range_example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating and initializing an array
        int[] arr = new int[10] {23, 45, 67, 78,
                        89, 34, 89, 43, 67, 89};
  
        Index start = 2;
        Index end = 5;
  
        // Creating range
        // Using Range(Index,
        // Index) Constructor
        var r = new Range(start, end);
        var value = arr[r];
  
        // Displaying range and elements
        Console.WriteLine("Range: " + r);
        Console.Write("Numbers: ");
  
        foreach(var i in value)
            Console.Write($" {i}, ");
    }
}
}
 
 

Output: 
 

Range: 2..5 Numbers:  67,  78,  89, 

Example 2:
 

CSharp




// C# program to illustrate how to
// use Range(Index, Index) constructor
using System;
  
namespace range_example {
  
class Program {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating and initializing an array
        string[] arr = new string[8] {"Archery", "Badminton",
                              "Cricket", "Bowling", "Boxing",
                       "Curling", "Tennis", "Skateboarding"};
  
        // Creating ranges
        // Using Range(Index,
        // Index) Constructor
        var r1 = new Range(0, 3);
        var r2 = new Range(4, 7);
        var value_1 = arr[r1];
        var value_2 = arr[r2];
  
        // Displaying range and elements
        Console.WriteLine("Range: " + r1);
        Console.Write("Sports Name: ");
  
        foreach(var i_1 in value_1)
            Console.Write($" {i_1} ");
  
        Console.WriteLine("\n\nRange: " + r2);
        Console.Write("Sports Name: ");
  
        foreach(var i_2 in value_2)
            Console.Write($" {i_2} ");
    }
}
}
 
 

Output:
 

Range: 0..3 Sports Name:  Archery  Badminton  Cricket   Range: 4..7 Sports Name:  Boxing  Curling  Tennis 

 



Next Article
C# | Operator Overloading
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-8.0

Similar Reads

  • Index Constructor in C#
    Index(Int32, Boolean) constructor of Index Struct in C# 8.0 is used to initialize a new index with the specified index position and a value which shows whether the index starts from the start or the end of the collection or sequence. If the specified index created from the end, then the index value
    3 min read
  • C# UInt32 Struct
    In C#, the UInt32 struct represents a 32-bit unsigned integer (commonly referred to as the uint data type). It is defined in the System namespace and provides various methods for mathematical computations, parsing, and type conversion. Because of it is an unsigned type, it only holds non-negative va
    2 min read
  • C# UInt16 Struct
    In C#, the UInt16 struct, defined under the System namespace, represents a 16-bit unsigned integer, commonly referred to as the ushort data type. It does not support negative values and provides a range from 0 to 65,535. The UInt16 struct inherits the ValueType class, which further inherits the Obje
    2 min read
  • C# UInt64 Struct
    In C#, the UInt64 struct represents a 64-bit unsigned integer, commonly referred to as the ulong data type. It is defined in the System namespace and provides various methods for mathematical computations, parsing, and type conversion. Since it is an unsigned type, it only holds non-negative values.
    3 min read
  • C# | Operator Overloading
    Prerequisite: Operators in C# The concept of overloading a function can also be applied to operators. Operator overloading gives the ability to use the same operator to do various operations. It provides additional capabilities to C# operators when they are applied to user-defined data types. It ena
    4 min read
  • C# ValueTuple Struct
    ValueTuple Struct in C# is a structure that provides static methods that are used in creating value tuples. It is defined under the System namespace and was introduced in .NET Framework 4.7. This struct enables runtime implementation tuples in C#. The ValueTuple structure represents a tuple that can
    4 min read
  • Int16.CompareTo() Method in C#
    Int16.CompareTo Method is used to compare the current instance to a specified object or another Int16 instance. It returns an integer which shows whether the value of the current instance is less than, equal to, or greater than the value of the specified object or the other Int16 instance. There are
    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
  • 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# 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
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