📌  相关文章
📜  在C#中查找范围从开始到结束的所有元素

📅  最后修改于: 2021-05-29 16:26:35             🧑  作者: Mango

范围结构在C#8.0中引入。它表示具有开始索引和结束索引的范围。您可以在Range结构提供的All Property的帮助下找到从开始索引到结束的所有范围对象。此属性始终返回0 .. ^ 0范围。

句法:

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

在此,Range代表从头到尾的索引。

范例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);
    }
}
}

输出:

0..^0

范例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}, ");
    }
}
}

输出:

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