Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Question about overriding ==

Posted on 2006-06-05
Medium Priority
Last Modified: 2010-04-16
To overload the == operator, must I override the Equals and GetHashCode methods inherited from the Object class? Or are they static?
Question by:igor92128
  • 2
  • 2
LVL 13

Accepted Solution

devsolns earned 2000 total points
ID: 16839439
You dont have to.  You overload them if you want to provide a different implementation for them.  Perhaps you may want to override equals to provide the same implementation as your overloaded == operator.  You should probably do this for consistency but again it is not required.

Author Comment

ID: 16839684
This Question was unclear, let me re-phrase it:

To overload the == operator, you must override

      a.       the Equals and GetHashCode methods inherited from the Object class.  
      b.       the static Equals and GetHashCode methods of the Object class.  
      c.       the Equals and GetHashCode properties inherited from the Object class.  
      d.       only the Equals method inherited from the Object class.  

Author Comment

ID: 16839912
it must be A...
LVL 13

Expert Comment

ID: 16841770
That is a tricky question here is why,

When you overload ==, C# insists that you also overload !=.

After overloading == & != C# will WARN you that you havent overriden the Equals() method.

After overloading ==, !=, & Equals() C# will then warn that you haven't overloaded GetHashCode().

That question is unfair because it says "you must override."  Is the must inclusive of warnings?  From those rather poor choices I'd say d. is the best fit.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question