Lecture overview -- Keyboard shortcut: 'u'  Previous page: Overview of reference types in C# -- Keyboard shortcut: 'p'  Next page: Equality in C# -- 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 5 : 29
Object-oriented Programming in C#
Reference types, Value types, and Patterns
Comparing and copying objects via references

This page introduces three types of equality: reference equality, shallow equality, and deep equality. A similar categorization is used for copying.

Do we compare references or the referenced objects?

Do we copy the reference or the referenced object?

How deep do we copy objects that reference other objects?

  • Comparing

    • Reference comparison

      • Compares if two references refers to objects created by the same execution of new

    • Shallow and deep comparison

      • Pair-wise comparison of fields - at varying levels

  • Copying:

    • Reference copying

    • Shallow or deep copying

      • Is also known as cloning

      • Somehow supported by the method MemberwiseClone in System.Object

 

An assignment of the form var = obj1 copies a reference

A comparison of the form obj1 == obj2 compares references (unless overloaded)