📌  相关文章
📜  查找指向C#中最后一个元素之外的索引

📅  最后修改于: 2021-05-29 17:11:19             🧑  作者: Mango

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

句法:

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

范例1:

// C# program to illustrate how 
// to get the End 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);
  
        // Getting the end index
        var res1 = Index.End;
  
        // Displaying the index
        Console.WriteLine("Index: {0}", in1);
        Console.WriteLine("Index: {0}", in2);
        Console.WriteLine("End Index: {0}", res1);
    }
}
}

输出:

Index: ^1
Index: 3
End Index: ^0

范例2:

// C# program to illustrate the
// concept of the End index
using System;
  
namespace example {
  
class GFG {
  
     // Main Method
    static void Main(string[] args)
    {
        string[] greetings = new string[] {"Hello", "Hola", "Namaste",
                                "Bonjour", "Ohayo", "Ahnyounghaseyo"};
  
        // Get the end index
        var res = Index.End;
  
        // Checking the given index
        // is the end index or not
        if (res.Equals (^0) == true) {
  
            Console.WriteLine("The given index is "+
                        "beyond the last element");
        }
  
        else {
  
            Console.WriteLine("The given index is not"+
                          " beyond the last element");
        }
    }
}
}

输出:

The given index is beyond the last element