TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C# Coding Standard
Search

Rule:  7@503

Synopsis:If you implement one of operator==(), the Equals method or GetHashCode(), implement all three
Language:C#
Severity Level:1
Category:Object oriented


Description:

If your Equals method can throw an exception, this may cause problems if objects of that type are put into a container. Do consider to return false for a null argument.

The msdn guidelines [MS Design] recommend to return false rather than throwing an exception when two incomparable objects, say the proverbial apples and oranges, are compared. Since this approach sacrifices the last remnants of type-safety, this recommendation has been weakened.

Exceptions:

In very rare cases it can be meaningful to override GetHashCode() without implementing the other two.

There is no need to implement the operator==() for reference types because a default implementation is available.