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

Rule:  7@526Checked automatically with code checker

This rule is Obsolete
Synopsis:Reference types should NOT override the equality operators ('==' and '!=')
Severity Level:1
Category:Object oriented


There's a very simple reason for this coding rule:

    The default implementation of the equality operators ('==' and '!=') on reference types check for reference equality !

So overriding this behavior introduces counter-intuitive behavior !

There are only these exceptions to this rule:

  • The String class has overridden the equality operators to provide value equality semantics (i.e. the String class returns true for '==' for any two class instances that contain exactly the same characters in the same order).
  • In case the reference type represents a value, e.g. class BigInt or class PositiveValue
  • For more detailed information read the corresponding (FxCop-checkable) MS Design Guidelines referred below.

    Literature References:
    MS Design Guidelines for Implementing Equals and the Equality Operator (==)