Facilitating the spread of knowledge and innovation in professional software development



Choose your language

InfoQ Homepage News Jonathan Allen on Equality Operator Overloading

Jonathan Allen on Equality Operator Overloading


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.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • What about IEqualityComparer?

    by Pierre Cart-Grandjean,

    Your message is awaiting moderation. Thank you for participating in the discussion.


    In your post you don't mention this interface. And I have to say that so many ways to define the equality puzzle me.


  • 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:

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.