Lecture overview -- Keyboard shortcut: 'u'  Previous page: Comparing and copying objects via references -- Keyboard shortcut: 'p'  Next page: Value Types [Section] -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 6 : 29
Object-oriented Programming in C#
Reference types, Value types, and Patterns
Equality in C#

There are several different equality operations in C#. This page provides an overview.

There are several equality operations in C# on reference types

 

  • o1.Equals(o2)     - equality

    • By default, true if o1 and o2 are created by execution of the same new

    • Can be redefined in a particular class

  • Object.ReferenceEquals(o1, o2)     - identity

    • True if both o1 and o2 are null, or if they are created by execution of the same new

    • Static - cannot be redefined.

  • Object.Equals(o1, o2)

    • True if Object.ReferenceEquals(o1, o2), or if o1.Equals(o2)

  • o1 == o2

    • True if both o1 and o2 are null, or if they are created by execution of the same new

    • An overloadable operator

Go to exerciseEquality of value types and reference types