C#.NET Frequently Asked Questions (and some short answers)


Q: What is the difference between a deep and a shallow copy?

Asking for a "copy" sets up the expectation that the new object will be a copy of the original not a pointer to the original. For value properties (e.g. original.MyInt) both a deep and a shallow copy satisfy that expectation: both will provide a copy of original that has a copy of the MyInt value. The difference between a shallow and deep copy happens with reference properties (e.g. original.MyClass). A shallow copy provides a copy of the reference to MyClass, which means both original.MyClass and copy.MyClass now point at the same instance. That is not a copy. A deep copy on the other hand provides an actual copy of MyClass and all its properties. The lack of symmetry of a shallow copy can confuse developers, because copy.MyInt is a copy of original.MyInt but copy.MyClass is not a copy of original.MyClass.

Q: How do we implement IEquatable<T> for value types?

Q: How do we implement IEquatable<T> for reference types?