📌  相关文章
📜  在C#中查找指定序列的第一个元素的索引

📅  最后修改于: 2021-05-30 00:15:43             🧑  作者: Mango

索引结构在C#8.0中引入。它表示可用于索引集合或序列的类型,并且可以从头开始或从头开始。您可以在Index结构提供的Start Property的帮助下找到索引,该索引指向指定集合或序列的第一个元素。

句法:

public static property Index Start { Index get(); };

范例1:

// C# program to illustrate the
// concept of the start index
using System;
  
namespace example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating new indexes
        // Using Index() constructor
        var in1 = new Index(1, true);
        var in2 = new Index(3, false);
  
        // Get the start index
        var res1 = Index.Start;
  
        // Displaying the index
        Console.WriteLine("Index: {0}", in1);
        Console.WriteLine("Index: {0}", in2);
        Console.WriteLine("Start Index: {0}", res1);
    }
}
}

输出:

Index: ^1
Index: 3
Start Index: 0

范例2:

// C# program to illustrate the
// concept of the start index
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"};
  
        // Get the end index
        var res = Index.Start;
  
        // Checking the given index 
        // is the start index or not
        if (res.Equals(0) == true) {
  
            Console.WriteLine("The given index is start index"+
                      " and the element is " + greetings[res]);
        }
  
        else {
  
            Console.WriteLine("The given index is not the start index ");
        }
    }
}
}

输出:

The given index is start index and the element is Hello