When using the object class's protected method memberwiseclone (), the msdn explanation is: Create a superficial copy of the current object. So what is "superficial copy? After reading the relevant information, the explanation is as follows:
A shallow copy of a collection copies only the elements of the collection, whether they are reference types or value types, but it does not copy the objects that the references refer. the references in the new collection point to the same objects that the references in the original collection point. in contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by the elements.
A simple copy of a set means that only the elements in the set are copied, no matter whether they are of the reference type or value type, but the referenced objects are not copied. This means that the reference in the new set and the reference in the original set refer to the same object. In contrast, the deep copy not only copies the elements in the set, but also copies all the objects directly or indirectly referenced by these elements. This means that the reference in the new set is different from the object referenced in the original set.
For the memberwiseclone method, the remarks on msdn are as follows:
The memberwiseclone method creates a superficial copy by creating a new object and then copying the non-static fields of the current object to the new object. If the field is of the value type, perform a step-by-step copy on the field. If the field is of reference type, the referenced object is copied but not referenced. Therefore, the original object and its counterparts reference the same object.
For example, an object named x references objects A and B. Object B References Object C. The child copy of X creates a new object X2, which also references objects A and B. In contrast, the deep copy of X creates a new object X2, which references the new objects A2 and B2, which are copies of A and B, respectively. B2 references the new object C2, which is a copy of C. Superficial or deep replication of the class execution object that implements the icloneable interface.
We also provide a SectionCodeAs an example:
Code
Using System;
Class Mybaseclass
{
Public Static String CompanyName = " My company " ;
Public Int Age;
Public String Name;
}
Class Myderivedclass: mybaseclass
{
Static Void Main ()
{
// Creates an instance of myderivedclass and assign values to its fields.
Myderivedclass M1 = New Myderivedclass ();
M1.age = 42 ;
M1.name = " Sam " ;
// Performs a shallow copy of M1 and assign it to m2.
Myderivedclass m2 = (Myderivedclass) m1.memberwiseclone ();
}
}