Is main reason why IEquatable<T>
interface was introduced because it allows you to do the same as the System.Object.Equals
method but without having to perform casts?
thank you
To make it type safe, I would think. Equals()
takes an object as parameter and hence you will not see an error if you pass in an object of wrong type until you run it.
One reason is so that you can require a class be equatable with a desired type, without necessarily being that type. E.g.
public void MyClass<T> where T : IEquatable<Foo>
{
private static readonly Foo SpecialFoo = Foo.SpecialFoo;
public void MyMethodThatProcessesTs(T theT)
{
if (theT.Equals(SpecialFoo))
{
// process theT.
}
}
}
In addition to what @Bala R says, it also avoids boxing when doing custom equality checking between structs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With