📜  C#中的范围构造函数

📅  最后修改于: 2021-05-29 15:24:34             🧑  作者: Mango

Range(Index,Index)构造函数是Range Struct的一部分。此构造函数用于创建Range的新实例以及指定的起始索引和结束索引。使用范围运算符或构造函数创建范围时,它将不会添加最后一个元素或结束索引元素。
例如,我们有一个数组{1、2、3、4、5、6},现在我们要打印range [1..3],那么它将打印2、3。它不打印2、3, 4,

句法:

public Range(Index start, Index end);

在此,开始代表范围的起始索引,而结束代表范围的最后索引。

范例1:

// 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 initailizing 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}, ");
    }
}
}

输出:

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

范例2:

// 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 initailizing 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} ");
    }
}
}

输出:

Range: 0..3
Sports Name:  Archery  Badminton  Cricket 

Range: 4..7
Sports Name:  Boxing  Curling  Tennis