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.

26 comments:

  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,
      Paul

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

    ReplyDelete
  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.


    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  6. 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

    ReplyDelete
  7. Nice blog!!!
    https://www.kaashivinfotech.com/internship-in-pune-for-computer-engineering-students
    Internships in pune for computer science students

    https://www.kaashivinfotech.com/internship-in-pune-for-computer-engineering-students
    Internships in pune for computer science students

    https://www.kaashivinfotech.com/tag/inplant-training-certificate-format-for-mechanical-engineering
    Inplant training certificate format
    www.kaashivinfotech.com/internship-for-ece-students-in-bangaloreInternships in bangalore for ece students

    https://www.kaashivinfotech.com/industrial-training-for-ece-engineering-students
    Industrial training for electronics and communication engineering students

    https://www.kaashivinfotech.com/internship-in-bangalore-for-be-cse-students
    Internship for computer science students in bangalore

    https://www.kaashivinfotech.com/internship-for-ece-students-in-bangalore
    Internship for ece students

    https://www.kaashivinfotech.com/internship-for-mba-students
    Mba internship in chennai
    https://www.kaashivinfotech.com/inplant-training-for-ece-students/">Inplant training in chennai for ece
    www.kaashivinfotech.com/internship-in-nagpur-for-computer-engineering-students/">Internship in nagpur for cse

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

    ReplyDelete
  9. It’s amazing to visit again n again coming to your blogs the superb effort is here.
    brand companies

    ReplyDelete


  10. Awesome blog. Thanks for sharing such a worthy information....
    Full Stack Development Tools
    Full stack Tools

    ReplyDelete
  11. This post is so interactive and informative.keep update more information...
    German Classes in Velachery
    German Classes in chennai

    ReplyDelete