9.8 Shallow and deep copy
Copying objects by value from one variable to another, rather than copying objects by reference (that is, replicating in the same way as structs) can be very complex. Because an object may contain references to many other objects, such as field members, this will involve many tedious operations. Copying each member from one object to another may not succeed, because some of the members may be reference types.
The. NET Framework considers this issue. Simply copying objects by member can be done by using the MemberwiseClone () method derived from System.Object, which is a protected method, but it is easy to define a public method on the object that invokes the method. The replication functionality provided by this method is called shallow copy (shallow copy) because it does not consider reference type members. Therefore, the reference member in the new object points to the object that is the same member in the source object, which in many cases is not ideal. If you want to create a new instance of a member (copying a value without copying a reference), you need to use deep copy at this point.
A icloneable interface can be implemented in a standard way. If you use this interface, you must implement the Clone () method that it contains. This method returns a value of type System.Object. We can use a variety of processing methods to execute the selected method body to get this object. Deep replication is possible if you prefer (but the execution process is not required, so you can perform a shallow copy as needed). See chapter 11th.
(original) C # learning note 09--defining class 08--shallow and deep replication