Home  >  Article  >  Backend Development  >  .NET Framework-Clone please introduce in detail

.NET Framework-Clone please introduce in detail

黄舟
黄舟Original
2017-03-18 13:16:531633browse

There are many objects in .NET that implement the IClonable interface, which means that they can implement the copy function, such as the ArrayList object (using C# Describe data structure 3: ArrayList), or write your own object that implements the IClonable interface.

See the introduction to the Clone method in ArrayList:

Create a shallow copy of System.Collections.ArrayList.

I’m very curious about the concept of shallow copy. After checking it on msdn, the explanation is a bit obscure. Shallow copying a collection refers to only copying the elements of the collection, regardless of whether the element is a value type or Reference type, but Clone does not copy the object (the object pointed to by the reference). In the newly cloned collection, the reference still points to the same object (the object pointed to by the reference in the original collection).

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

In one sentence, the so-called shallow copy implemented by Clone, the object copied from Clone has the value Type, the reference is copied but the reference object is not copied. At this time, you may have to ask, what does it mean that the reference object is not copied? The problem is best explained through code. Please see the following code.

        //人员对象模型
        public class Person
        {            public string name { get; set; }            
        public ContactInfo description { get; set; }    

            public Person(string name, ContactInfo description)
            {                this.description = description;                
            this.name = name;
            }
        }        //联系信息对象
        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;
            }            
            //跟新电话联系信息
            public void UpdateTelephone(string telephone)
            {                
            this.telephone = telephone;
            }
        }

Create a new ArrayList object and add it as a reference object and a value type data.

             //ArrayList对象
            ArrayList arr1 = new ArrayList();            
            //Person对象创建,xiaoming引用Person对象
            Person xiaoming = new Person("xiaoming",new ContactInfo("shanghai","18011113333"));            
            //arr1引用xiaoming,这样arr1[0]也引用了Person对象
            arr1.Add(xiaoming);            
            //arr1中添加值类型整形5元素
            arr1.Add(5);

We use the Clone interface , Clone a shallow copy of arr1:

ArrayList cloneArr1 = arr1.Clone() as ArrayList;

As shown in the figure:


.NET Framework-Clone please introduce in detail

Test separately, clone What has been copied by the cloneArr1 instance? We check the copying of value types and reference types respectively. Let’s first look at the copying of value types:
cloneArr1[1]=6;

Let’s check whether the elements of the initial set arr1[1] have changed?
 has not changed, and the value is still 5. This shows that after Clone, the value type is also copied and placed on the memory stack.

Check whether the reference type has been re-opened from the memory heap, and modified xiaoming's contact number - Tel:

 (cloneArr1[0] as Person).description.UpdateTelephone("170444455555");

At this time, we will check again, the contact information of xiaoming in the initial set arr1 Has it changed?
Answer: Changed, consistent with the latest 170444455555.
This shows that for reference types, shallow copy copy only copies the reference without re-allocating memory space in the memory heap. (If you are not very clear about the concepts of memory heap and 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 copy, reference type only copies the reference, not the object. !

So what if I want to achieve deep copying, that is, my newly copied object is not just a copy of the reference, but a copy of the object! For example, you need to modify the creation of 5 versions based on a template, and each version is delivered to a different company. Version 1 is delivered to company A, and version 2 is delivered to company B. . . Suppose the difference between these five versions is just "I hope to join such-and-such company", and so-and-so is replaced by the corresponding names of the five companies.

The above is the detailed content of .NET Framework-Clone please introduce in detail. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn