📌  相关文章
📜  在C#中检查给定索引是否相等

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

索引结构在C#8.0中引入。它表示可用于索引集合或序列的类型,并且可以从头开始或从头开始。您可以在Index结构提供的以下方法(Equal Method )的帮助下比较两个索引,以检查它们是否相等:

1.等于(指数)

此方法返回一个值,该值表明给定的对象等于另一个对象。如果返回true,则当前结果为布尔值,则当前对象等于另一个对象;如果返回false,则当前对象不等于另一个对象。

句法:

public virtual bool Equals(Index other);

例子:

// C# program to illustrate the
// concept of the Equals(index) 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"};
   
        // Get the end index
        var res = Index.Start;
   
        // Checking the given index 
        // is the start index or not
        // Using Equals(index) method
        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

2.等于(对象)

此方法返回一个值,该值表明给定的索引对象等于另一个索引对象。如果返回true,则当前结果为bool形式,则当前索引对象等于另一个索引对象;如果返回false,则当前索引对象不等于另一个索引对象。

句法:

public override bool Equals(System::Object ^ value);

例子:

// C# program to illustrate the concept
// of the Equals(object) 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 index
        // Using Index() constructor
        var val1 = new Index(1, true);
        var val2 = new Index(2, false);
  
        // Checking the given both the
        // index values are equal or not
        // Using Equals(object)
        if (val1.Value.Equals(val2.Value) == true) {
  
            Console.WriteLine("Both the indexes are equal and"+
              " their elements are :{0}, {1}", greetings[val1], 
                                              greetings[val2]);
        }
        else {
  
            Console.WriteLine("Both the indexes are not equal and"+
                  " their elements are: {0}, {1}", greetings[val1],
                                                  greetings[val2]);
        }
    }
}
}

输出:

Both the indexes are not equal and their elements are: Ahnyounghaseyo, Namaste