(original) C # learning note 09--defining class 08--shallow and deep replication

Source: Internet
Author: User
Tags shallow copy

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

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.