I've read in several articles that
for reference types using IEquatable reduces the use of casting
Can someone kindly provide a convincing example.
Thanks
Clarry
I've read in several articles that
for reference types using IEquatable reduces the use of casting
Can someone kindly provide a convincing example.
Thanks
Clarry
I believe your answer is here, http://msdn.microsoft.com/en-us/library/ms131190.aspx. Read the remarks section on the page.
Do you know this article? It says
If you want to have your own implementation, you can override this method. Since the Equals method has a parameter of type Object, a cast will be needed in order to be able to access class specific members.
This is where the IEquatable interface comes in. The IEquatable is a new generic interface in .NET 2.0 that allows you to do the same as the System.Object.Equals method but without having to perform casts. So, when implementing this interface, you can reduce the number of casts, which is a good thing for performance. Especially when working a lot with generic collections, since generic collections make use of this equality comparison in some of their methods (List.Equals(), List.IndexOf(), List.LastIndexOf(), ...).
Due to its generic type parameter, IEquatable<T>
can provide type-checking at compiletime so you don't have to cast and late-bind with object
s.
Just to add a simple example after Dario's explanation:
class Person : IEquatable<Person>
{
public string Name { get; set; }
public override bool Equals(object obj)
{
if (obj is Person)
{
Person other = (Person)obj;
// check equality
}
return base.Equals(obj);
}
#region IEquatable<Person> Members
public bool Equals(Person other)
{
// check equality without cast
}
#endregion
}
Note: This is just a little fragment that shows why this avoids a cast
. For a correct implementation check the docs:
If you implement IEquatable<(Of <(T>)>), you should also override the base class implementations of Object..::.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable<(Of <(T>)>)..::.Equals method