C # Deep copy and light copy

Source: Internet
Author: User

The difference between the shallow copy and the deep copy is that the processing value type is the same as the processing reference type. When the value type is processed, both of the two copy modes directly allocate a new memory area and copy the value type value at the same time. When processing the reference type, the shortest copy only copies its address, instead of generating a new object. A new object is required for deep copy, and the value of the original object is copied to the new object.

 

Implementation Method:

1. For value types:
Shallow copy: This method is directly implemented by assigning values to a new object.
Deep copy: this function is directly implemented by assigning values to a new object. The same as the shortest copy.

2. For reference types:
Value Type: The Object. MemberwiseClone method creates a shortest copy. The method is to create a new Object. If the field is of the value type, the field is copied one by one. If the field is of reference type, copy and reference the original object, and the original object references the same object.


Reference Type: copy the object application, or copy the actual content of the object, that is, creating a new object does not affect the content of the original object. In this case, you need to implement the Clone method provided in the ICloneable interface.


The difference is that the mechanism is different when the implementation of the Reference type is deep copy and shallow copy. The former is the implementation of the MemberwiseClone method, and the latter is the Clone method provided in the ICloneable interface through inheritance, implements in-depth object copying.

Related Article

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.