In this deep dive article on equal operator overloading Jonathan Allen clears the air on overriding the equality operator. In the article Jonathan provides code samples in both VB and C# to demonstrate the nuances of each .NET language. He also covers usage in both structures and classes.
Areas covered include:
- The initial Class signature
- Fields and Properties
- Type-Safe Equality
- Hash Codes
- Overriding the base class Equals method
- Performance and Testing
Enjoy this well-thought through tutorial on Equality Operator overloading.
Community comments
What about IEqualityComparer?
by Pierre Cart-Grandjean,
When to do this
by Mat Hobbs,
Index Operator Overloading
by Shawn Zernik,
What about IEqualityComparer?
by Pierre Cart-Grandjean,
Your message is awaiting moderation. Thank you for participating in the discussion.
Hello,
In your post you don't mention this interface. And I have to say that so many ways to define the equality puzzle me.
Cheers,
Timok
When to do this
by Mat Hobbs,
Your message is awaiting moderation. Thank you for participating in the discussion.
You mention "Operator overloading usually makes more sense in structures than in classes..." but it would be useful to point out that for classes then almost never do you want to override Equals() or GetHashCode() except in very specific circumstances. This gets abused a lot...
Index Operator Overloading
by Shawn Zernik,
Your message is awaiting moderation. Thank you for participating in the discussion.
The following is how to overload the index operator:
www.internetworkconsulting.net/how-override-ind...