Equality for Different Types

Equality checks are of particular interest. For reference types, the default behavior of == and != is to check equality and inequality of the references. In other words, do both the left and right sides of the operator refer to the same object in memory? Often this behavior is not desirable, and operator overloading is used to redefine the meaning of equality, typically based on the underlying data contained by the type. A good example is System.String, where == checks that both strings have the same length and contain the same characters.

In the context of reference types and nullable value types, the equality checks against the null literal should also be considered. For reference types, this is based on checking ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.