. NET Framework-clone details

Source: Internet
Author: User
. NET has many objects that implement the IClonable interface, which means that they can implement replication functions, such as ArrayList objects (which describe the data structure in C # 3:arraylist), or write their own objects that implement the IClonable interface.

See the introduction to the Clone method in ArrayList:

Creates a shallow copy of the System.Collections.ArrayList.

Very curious, the concept of superficial replicas, after MSDN lookup, explains the meaning of the more obscure, shallow copy collection refers to copying only the collection elements, regardless of whether the element is a value type or a reference type, but clone does not copy the object (the object to which the reference refers). In the collection after the new clone, the reference still points to the same object (the object referenced in the original collection).

A shallow copy of a collection copies only the elements of the collection, whether they is reference types or value types , but it does not copy the objects, the references refer to. The references in the new collection point to the same objects, the references in the original-collection point-to.

In a nutshell, the so-called shallow copy implemented by clone, clone out of the object copied the value type, copy the reference, and not copy the Reference object. At this point, you might ask, what does it mean to not copy a reference object? The code is the best way to explain the problem, please look at the following code,

//Personnel object Model public class people {public string name { Get Set                Public ContactInfo Description {get; set;}                            Public person (string name, ContactInfo description) {this.description = description;            THIS.name = name;                    }}//Contact information Object public class ContactInfo {public string address {get; set;}                    public string Telephone {get; set;}                            Public ContactInfo (string address, string telephone) {this.address = address;            This.telephone = telephone;                            }//contact information for new phone public void Updatetelephone (string telephone) {            This.telephone = telephone; }        }

Creates a new ArrayList object and adds it as a reference object, one value type data

             ArrayList object            ArrayList arr1 = new ArrayList ();                        The person object is created, xiaoming references the person object person            xiaoming = new Person ("Xiaoming", New ContactInfo ("Shanghai", "18011113333 "));                        ARR1 references xiaoming, so arr1[0] also refers to the person object            arr1. ADD (xiaoming);                        ARR1 adds value type shaping 5 element            arr1. ADD (5);

Through the clone interface, we clone a shallow copy of the arr1:

ArrayList cloneArr1 = arr1. Clone () as ArrayList;





Separately, the cloned instance cloneArr1 what we have copied, and we check the replication of the value type and reference type separately. First look at the replication of value types:
clonearr1[1]=6;

Do we check that the elements of the initial set arr1[1] have changed?
  unchanged , the value is still 5, which means that after cloning, the value type is also copied and placed in the memory stack.

Check under Reference type there is no re-opening space from the memory heap, modify xiaoming contact-Phone:

(clonearr1[0] as person). Description. Updatetelephone ("170444455555");

At this time, we check again, the initial collection arr1 Xiaoming contact method changed it?
Answer: Changed, and the latest 1,704,444,555,551 to.
This illustrates that a shallow copy of a reference type, just a copy of a reference, does not re-open memory in the memory heap. (If you are not very clear about the memory heap, memory stack, please refer to my summary: C #.) NET: Memory management story-variable creation and destruction).

At this point, we have a new understanding of the function of clone! Shallow copies, reference types only copy references, and do not copy objects.

So if you say, I want to achieve deep replication, that is, my newly copied object is not just copy the reference, but copy the object! For example, you need to base on a template to modify the 5 version of the establishment, each version posted to different enterprises, version 1 to the company A, version 2 to the Company B, ... If the 5 version of the difference is only "I expect to join a company," and so on to 5 companies corresponding to the name.

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.