Checking the Given Indexes are Equal or not in C# - GeeksforGeeks

28 Nov, 2019
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:
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) methodusing 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
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) methodusing 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
CSharp-8.0
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
C# | Inheritance
C# | List Class
Partial Classes in C#
Convert String to Character Array in C#
Lambda Expressions in C#
Difference between Hashtable and Dictionary in C#