Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effective C#: Overriding Object.Equals(), yay or nay?

In the second edition of Effective C# (ISBN-13: 978-0321658708) on page 37, the book reads

The second function you'll never redefine is static Object.Equals()

However, on page 39, the book reads

The point is that if your type should follow value semantics (comparing contents) instead of reference semantics (comparing object identity), you should write your own override of instance Object.Equals()

Would someone be so kind as to explain why one would override

public virtual bool Equals(object right);

and not

public static bool Equals(object left, object right);

Thank you :)

like image 810
Nick Carlson Avatar asked Dec 17 '22 14:12

Nick Carlson


1 Answers

Because you can't override a static method.

like image 63
Steven Avatar answered Jan 02 '23 16:01

Steven