Comprobación de que los índices dados son iguales o no en C#

La estructura de índice se introduce en C# 8.0. Representa un tipo que se puede usar para indexar una colección o secuencia y se puede iniciar desde el principio o el final. Puede comparar dos índices con cada uno para verificar si son iguales o no con la ayuda de los siguientes métodos ( Equal Method ) proporcionados por la estructura del índice:

1. Igual (Índice)

Este método devuelve un valor que muestra que el objeto dado es igual al otro objeto. El resultado tiene la forma de bool si devuelve verdadero, entonces el objeto actual es igual a otro objeto o si devuelve falso, entonces el objeto actual no es igual a otro objeto.

Sintaxis:

public virtual bool Equals(Index other);

Ejemplo:

// 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 ");
        }
    }
}
}

Producción:

The given index is start index and the element is Hello

2. Igual (Objeto)

Este método devuelve un valor que muestra que el objeto de índice dado es igual al otro objeto de índice. El resultado tiene la forma de bool si devuelve verdadero, entonces el objeto de índice actual es igual a otro objeto de índice o si devuelve falso, entonces el objeto de índice actual no es igual a otro objeto de índice.

Sintaxis:

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

Ejemplo:

// 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]);
        }
    }
}
}

Producción:

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

Publicación traducida automáticamente

Artículo escrito por ankita_saini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *