📌  相关文章
📜  如何检查索引是从C#的开头还是结尾?

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

索引结构在C#8.0中引入。它表示可用于索引集合或序列的类型,并且可以从头开始或从头开始。您可以在Index结构提供的IsFromEnd属性的帮助下从给定集合或序列的开头或结尾检查给定索引。如果IsFromEnd属性返回false,则表示索引从头开始;如果IsFromEnd属性返回true,则意味着索引从头开始。]

句法:

public property bool IsFromEnd { bool get(); };

范例1:

// C# program to illustrate the
// concept of the IsFromEnd property
using System;
  
namespace example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating new indexes
        // Using Index() constructor
        var val1 = new Index(1, true);
        var val2 = new Index(2, true);
        var val3 = new Index(1, false);
        var val4 = new Index(2, false);
  
        // Checking if the specified 
        // index start from the end
        // or not
        var res1 = val1.IsFromEnd;
        var res2 = val2.IsFromEnd;
        var res3 = val3.IsFromEnd;
        var res4 = val4.IsFromEnd;
  
        // Display indexes and their values
        Console.WriteLine("Index:{0} Start from end?: {1}", val1, res1);
        Console.WriteLine("Index:{0} Start from end?: {1}", val2, res2);
        Console.WriteLine("Index:{0} Start from end?: {1}", val3, res3);
        Console.WriteLine("Index:{0} Start from end?: {1}", val4, res4);
    }
}
}

输出:

Index:^1 Start from end?: True
Index:^2 Start from end?: True
Index:1 Start from end?: False
Index:2 Start from end?: False

范例2:

// C# program to illustrate the
// concept of the IsFromEnd property
using System;
  
namespace example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        string[] greetings = new string[] {"Hello", "Hola", "Namaste", 
                                "Bonjour", "Ohayo", "Ahnyounghaseyo"};
  
        var val1 = new Index(1, true);
  
        // Checking the given index
        // is the end index or not
        if (val1.IsFromEnd == true) {
            Console.WriteLine("The given index is from end "+
                     "and the value is: " + greetings[val1]);
        }
        else {
            Console.WriteLine("The given index is from start and"+
                             " the value is: " + greetings[val1]);
        }
    }
}
}

输出:

The given index is from end and the value is: Ahnyounghaseyo