Question about overriding ==

To overload the == operator, must I override the Equals and GetHashCode methods inherited from the Object class? Or are they static?
Who is Participating?
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.
igor92128Author Commented:
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.  
igor92128Author Commented:
it must be A...
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.