Skip to content
Related Articles

Related Articles

Finding all the Elements of a Range from Start to End in C#
  • Last Updated : 28 Nov, 2019

The Range Structure is introduced in C# 8.0. It represents a range that has a start and end indexes. You are allowed to find all the range object starting from the start index to end with the help of All Property provided by the Range struct. This property always returns 0..^0 range.

Syntax:

public static property Range All { Range get(); };

Here, Range represents the index from start to end.

Example 1:




// C# program to illustrate the use of the 
// All property of the Range struct
using System;
  
namespace range_example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating range
        // using Range Constructor
        var r = new Range(0, 5);
  
        // Getting the range objects from 
        // the starting index to end
        // Using All property
        var new_r = Range.All;
        Console.WriteLine(new_r);
    }
}
}

Output:



0..^0

Example 2:




// C# program to illustrate how to use 
// All property of the Range struct
using System;
   
namespace range_example {
   
class GFG {
   
    // Main Method
    static void Main(string[] args)
    {
        // Creating and initailizing an array
        int[] arr = new int[10] {23, 45, 67, 78,
                        89, 34, 89, 43, 67, 89};
   
        // Finding all the range
        // Using All Property 
        // of Range struct
        var value = Range.All;
        var a = arr[value];
   
        // Displaying range and elements
        Console.WriteLine("Range: " + value);
        Console.WriteLine("Numbers: ");
   
        foreach(var i in a)
            Console.Write($"{i}, ");
    }
}
}

Output:

Range: 0..^0
Numbers:
23,  45,  67,  78,  89,  34,  89,  43,  67,  89, 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :