Deep copy and shallow copy
1. Deep copy means that the source object and the copy object are independent of each other. Any changes to one object will not affect the other object. For example, a person named Zhang San was cloned with him (assuming the law permits it) and called Li Si. Neither Zhang San's arm nor legs nor Li Si's arm nor legs would affect another person. Typical objects are Value objects, such as Int32, Double, struct, and Enum.
Consider the following statement:
Int source = int. MaxValue; // (1) initialize the maximum value of the source object as an integer: 2,147,483,647
Int dest = source; // (2) assign a value and execute deep copy internally.
Dest = 1024; // (3) assign values to the Copied object
Source = 2048; // (4) assign values to the source object
First (2) Assign source to dest and execute the deep copy action. At that time, the values of dest and source are the same, both of which are int. maxValue; (3) modify the dest, and the value of dest changes to 1024. Because it is a deep copy, the source is not run, and the source is still int. maxValue; (4) the source is modified. Similarly, dest is still 1024, and int. the value of MaxValue remains unchanged, which is still 2,147,483,647; only source is changed to 2048.
Consider writing the following code:
Struct Point
{
Public int X;
Public int Y;
Public Point (int x, int y)
{
X = x;
Y = y;
}
}
Point source = new Point (10, 20 );
Point dest = source;
Dest. X = 20
After the dest. X attribute is changed to 20, the X attribute of source is still 10.
2. Shallow copy refers to the sharing of an object between the source object and the copy object, but only the referenced variables are different (different names ). Any changes to one of the objects will affect another object. For example, a person was originally named Zhang San and later renamed Li Si, but he was still the same person. Whether Zhang San lacks his arm and legs or Li Si lacks his arm and legs, this person is unlucky. A typical example is a Reference object, such as a Class ).
Consider the following statement:
Class Point
{
Public int X;
Public int Y;
Public Point (int x, int y)
{
X = x;
Y = y;
}
}
Point source = new Point (10, 20 );
Point dest = source;
Dest. X = 20;
Because Point is now a reference object, the value assignment of Point dest = source actually executes a shortest copy, and the final result should be that the value of source's X field has also changed to 20. That is to say, they reference the same object, but the variables are different from those of 'source' and 'dest.
3. Principle of copying objects
The assignment between referenced objects is performed by the shortest copy action, which is related to the characteristics of referenced objects. A referenced object is generally composed of two parts.
(1) A named Handle, which is what we call a declaration (such as a variable)
(2) An internal (not named) object, that is, the internal object of the named Handle. It is allocated in the Manged Heap (managed Heap). Generally, the New method for adding a referenced object is to create www.2cto.com.
If the internal object has been created, the named Handle points to the address of the internal object in the Manged Heap. Otherwise, it is null (in a certain aspect, if the value of handle can be null, it indicates that it is a reference object, of course not absolute ). If two referenced objects are assigned values, they only copy the address of the internal object and the internal object is still the same. Therefore, modifications to the source object or the copy object will affect the other object. This is the shortest copy.
4. How to make a deep copy of the referenced object
Because the value assignment of the referenced object only copies the address pointed to by the named Handle (variable), to make a deep copy of the referenced object, you must recreate an instance of the object, assign values to the fields of the object one by one, as shown in the following code:
Class Point
{
Public int X;
Public int Y;
Public Point (int x, int y)
{
X = x;
Y = y;
}
}
Point source = new Point (10, 20 );
Point dest = new Point (source. X, source. Y );
// Or the following statement
// Point dest = new Point ()
// Dest. X = source. X
// Dest. Y = source. Y
At that time, source and dest are two mutually independent objects, and their modifications will not affect each other.
5. Notes
(1): A String object is a reference object, but it is very special. It is represented like a value object, that is, it is assigned a value, divided, and merged, instead of operating on the original string, A New String object is returned.
(2): The Array object is a reference object. When assigning values, another reference of the source object is actually returned; therefore, if you want to copy an array object (deep copy), you need to create a new set of objects, and then copy the values of the source array to the target object one by one.
From cloud huaikong-abel