📜  在C#的集合开始时从指定的索引创建索引

📅  最后修改于: 2021-05-29 22:18:00             🧑  作者: Mango

索引结构在C#8.0中引入。它表示可用于索引集合或序列的类型,并且可以从头开始或从头开始。您可以在Index结构提供的FromStart(Int32)Method()的帮助下创建起始索引。此方法从给定集合或序列的开头在指定位置返回一个索引。

句法:

public static Index FromStart(int value);

范例1:

// C# program to illustrate the 
// concept of the FromStart() method
using System;
  
namespace example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating start index
        // Using FromStart() method
        var in1 = Index.FromStart(2);
        var in2 = Index.FromStart(1);
        var in3 = Index.FromStart(0);
        var in4 = Index.FromStart(6);
  
        // Displaying  index value
        Console.WriteLine("Index position is : {0} ", in1);
        Console.WriteLine("Index position is : {0} ", in2);
        Console.WriteLine("Index position is : {0} ", in3);
        Console.WriteLine("Index position is : {0} ", in4);
    }
}
}

输出:

Index position is : 2 
Index position is : 1 
Index position is : 0 
Index position is : 6 

范例2:

// C# program to illustrate the 
// concept of the FromStart() method
using System;
  
namespace example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // Creating and initializing an array
        string[] greetings = new string[] {"Hello", "Hola", "Namaste", 
                                "Bonjour", "Ohayo", "Ahnyounghaseyo"};
  
        // Creating start index
        // Using FromStart() method
        var index_1 = Index.FromStart(0);
        var index_2 = Index.FromStart(1);
        var index_3 = Index.FromStart(4);
        var index_4 = Index.FromStart(5);
  
        // Displaying index and their values
        Console.WriteLine("Index: {0} Value: {1}",
                    index_1, greetings[index_1]);
  
        Console.WriteLine("Index: {0} Value: {1}",
                    index_2, greetings[index_2]);
  
        Console.WriteLine("Index: {0} Value: {1}",
                    index_3, greetings[index_3]);
  
        Console.WriteLine("Index: {0} Value: {1}",
                    index_4, greetings[index_4]);
    }
}
}

输出:

Index: 0 Value: Hello
Index: 1 Value: Hola
Index: 4 Value: Ohayo
Index: 5 Value: Ahnyounghaseyo