Open In App

Checking the Given Indexes are Equal or not in C#

Last Updated : 28 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The Index Structure is introduced in C# 8.0. It represents a type that can be used to index a collection or sequence and it can be started from the start or the end. You are allowed to compare two indexes with each to check whether they are equal or not with the help of the following methods(Equal Method) provided by the Index struct:

1. Equals(Index)

This method returns a value that shows that the given object is equal to the other object. The result is in the form of bool if it returns true, then the current object is equal to another object or if it returns false, then the current object is not equal to another object.

Syntax:

public virtual bool Equals(Index other);

Example:




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


Output:

The given index is start index and the element is Hello

2. Equals(Object)

This method returns a value that shows that the given index object is equal to the other index object. The result is in the form of bool if it returns true, then the current index object is equal to another index object or if it returns false, then the current index object is not equal to another index object.

Syntax:

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

Example:




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


Output:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads