. NET Deep Learning Notes (4): Deep copy and shallow copy (Deep copy and shallow copy)

Source: Internet
Author: User
Tags resource shallow copy

Today continue to use the preparation of WSE security development articles free time, perfect. NET Deep Learning Notes series (Basic). NET important points of knowledge, I have done a detailed summary, what, why, and how to achieve. Presumably many people have been exposed to these two concepts. People who have done C + + will not be unfamiliar with the concept of deep copy. And many of its C # Senior software engineers interviewed also asked about the depth of copy-related issues. I'm summarizing it today and adding a detailed code implementation to share with you. Learn about the mechanics of C # 's deep and shallow copies (Deep copy and shallow copy). The full text is divided into three parts: 1. Basic Concepts 2. Deep copy and shallow copy implementation Mechanism 3. Code implementation and Analysis 4. physiognomy us to enter the formal study.

1. Basic Concepts:

First we should understand what is called deep copy and shallow copy (Deep copy and shallow copy).

A. Shallow copy (shallow copy Shadow clone): Copy only the base type of the object, the object type, and still belong to the original reference.

B. Deep copy (Deep copy depth clone): does not compact the base class of the object and also copies the objects in the original object. Completely generate new objects.

We know that in C + + there is the concept of copy constructors and copy assignment functions. A shallow copy is a one by one assignment between member data: Assign the value to one by one to assign to the value to be copied. But there may be cases where the object also contains resources, where the resources can be value heap resources, or a file. When a copy of a duty is available, two objects have a common resource and access to the resource, which can cause problems. Deep copy is used to solve such problems, it assigns resources to the same time, so that objects have different resources, but the content of the resource is the same. For the heap resources, is to open up a heap of memory, the original content copy.

If you copy an object that references an external content (such as the data allocated on the heap), so when you copy this object, let both the old and the new object point to the same external content, which is a shallow copy; If you copy this object, you make a separate copy of the external object for the new object, which is a deep copy.

The concept in this C # is similar to C + +. We can refer to the previous concept understanding. The difference between a deep copy and a shallow copy can be seen from a definition. A shallow copy is the copying of a field of a numeric type in an object to a new object, whereas a reference field in an object refers to copying one of its references to the target object. If you change the value of a reference field in the target object, he will be reflected in the original object, which means that the corresponding field in the original object will also change.

A deep copy differs from a shallow copy in the processing of a reference copy, which is created in a new object and is a field of the corresponding value type in the object and is assigned a value. A shallow copy does not create a new reference type and returns the same type reference. A deep copy will recreate the new object, returning the reference word for the new object. The C # Heavy observer pattern is an example of a shallow copy. We keep just a copy of the object.

2. Deep copy and light copy implementation mechanism:

From the above concept we learned the difference between C # deep copy and shallow copy (Deep copy and shallow copy). This also determines that the two have different ways to achieve.

For value types:

A. Shallow copy: Directly implemented by assigning values and so on, copying fields of value types in the object to new objects.

B. Deep copy: Directly implemented by assignment, and copying fields of value types in the object to the new object. Same as shallow copy

For reference types:

A. Value type: The MemberwiseClone method creates a shallow copy by creating a new object that performs a bitwise copy of the field if the field is a value type. If the field is a reference type, the copy references the original object, referencing the same object as the original object.

B. Reference type: Copy object application, also copy object actual content, that is, create a new change object does not affect the original object's content

This situation requires that it implement the Clone method provided in the ICloneable interface.

The difference is that there is a different mechanism for deep copy and shallow copy of reference type, the former is MemberwiseClone method implementation, the latter is to implement the Clone method provided in ICloneable interface through inheritance, and realize deep copy of object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.