Please navigate to the bottom of the page for Table of Contents

Friday, May 27, 2011

Operator overloading and pairing rules in C#

What is the problem in the following code snippet?

namespace ConsoleApplication1
struct Point
int X;
int Y;

public static bool operator ==(Point p1, Point p2)
return ((p1.X == p2.X) && (p1.Y == p2.Y));

When you overload equality (==, !=) and comparison (<, >, <=, >=) operators, special pairing and overriding rules are enforced by C# compiler. For the above example, the compiler generates 2 warnings and the 1 error:

  • Warning: 'ConsoleApplication1.Point' defines operator == or operator != but does not override Object.GetHashCode()

  • Warning: 'ConsoleApplication1.Point' defines operator == or operator != but does not override Object.Equals(object o)

  • Error: The operator 'ConsoleApplication1.Point.operator ==(ConsoleApplication1.Point, ConsoleApplication1.Point)' requires a matching operator '!=' to also be defined   

Rule 1: If you overload the equality operators (== and !=), it is good practice to override the GetHashCode() and Equals() methods. Reason: Collections and HashTables rely on equality relations to work reliably.

Rule 2: If you overload one operation that is part of a pair (for example, == and !=), you are required to implement the other operator.

Rule 3: If you overload the comparison operators (< and >), it is a good practice to implement IComparable and IComparable<T> for the same reasons defined for rule 1.


  1. Replies
    1. Hi Nikhil,

      Allow me to show my gratitude bloggers. You guys are like unicorns. Never seen but always spreading magic. Your content is yummy. So, satisfied.
      I want to minimize this string "AHHHAAAAARTFUHLAAAAAHV" to "AHHHAARTFUHLAHV". Removing excessive "A's" would be my only option. I tried using a split function (with a delimiter of "AAAA[^A]") to make it look like this [AHHHA, AAAA, RTFUHLAA, AAAA, HV]. But now I’m at lost how to manipulate those indices with "AAAA"s now to arrive to my desired output.
      In order to do design level changes, first of all design review needs to be done. The task becomes more difficult if the original architect of the application is not involved. All aspects of the design should be minutely analysed and then different approaches should be considered. Some general principles are listed below.
      Anyways great write up, your efforts are much appreciated.

      Many Thanks,

  2. Can we have a correct code for the above code snippet please?

  3. There are all the way the bewts Boneka Wisuda of optimization and the best marketing which you can think about.
    Backlinks is just like brand After Boneka Wisuda.
    looking into a handful of the blog posts on your blog, Boneka Wisuda
    I truly like your technique of blogging. Boneka Wisuda I book marked it to my bookmark site list and will be checking back soon. Please visit my
    web site as well and let me know how you feel.Boneka Wisuda advertising which carried out offline throuh press Boneka Wisuda.

  4. Nice Blog.Thank you for Sharing. We are leading erp software software solution providers in chennai.
    erp in chennai | cloud erp software in chennai

  5. You are doing a great job. You inspire me to write for other. Thank you very much. I would like to appreciate your work for good accuracy and got informative knowledge from here. Python Training in Chennai

  6. I found a lot of interesting information here. A really good post
    office 2010 professional plus key deutsch

  7. Your blog is so inspiring for the young generations.thanks for sharing your information with us and please update more new ideas.
    German Training in Guindy
    german training institutes in bangalore
    german classes in bangalore
    german language course in bangalore

  8. good..I would like to thank you for sharing this valuable information
    big data training in chennai

  9. awesome blog.

  10. Nice blog!!!
    Internships in pune for computer science students
    Internships in pune for computer science students
    Inplant training certificate format in bangalore for ece students
    Industrial training for electronics and communication engineering students
    Internship for computer science students in bangalore
    Internship for ece students
    Mba internship in chennai">Inplant training in chennai for ece">Internship in nagpur for cse

  11. This post is really nice and informative. The explanation given is really comprehensive and useful.... business intelligence tutorial for beginners

  12. Excellent blog!!! I got to know more useful information by reading your blog. Thanks for posting this blog.

    Apache Spark Training in Pune
    Spark Training Institute in Pune

  13. I am really happy with your blog because your article is very unique and powerful for new.
    3D Scanning Services Near Me
    3D Laser Scanning Targets
    3D Scanning Reverse Engineering

  14. This is most informative and also this post most user-friendly and super navigation to all posts.
    Data Science Course in Pune
    Python Classes in Pune
    Best AWS Training in Pune