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:
using System;
namespace example {
class GFG {
static void Main( string [] args)
{
string [] greetings = new string [] { "Hello" , "Hola" , "Namaste" ,
"Bonjour" , "Ohayo" , "Ahnyounghaseyo" };
var res = Index.Start;
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:
using System;
namespace example {
class GFG {
static void Main( string [] args)
{
string [] greetings = new string [] { "Hello" , "Hola" , "Namaste" ,
"Bonjour" , "Ohayo" , "Ahnyounghaseyo" };
var val1 = new Index(1, true );
var val2 = new Index(2, false );
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Nov, 2019
Like Article
Save Article