DeepClone與ShadowClone

來源:互聯網
上載者:User
杳MSDN知:
ArrayList.Clone 方法

建立ArrayList 的淺表副本。

命名空間:System.Collections
程式集:mscorlib(在 mscorlib.dll 中)

傳回值ArrayList 的淺表副本。

集合的淺表副本僅複製集合的元素(不論它們是參考型別還是實值型別),但不複製引用所引用的對象。新集合中的引用與原創組合中的引用指向相同的對象。

與之相對,集合的深層副本將複製這些元素以及由它們直接或間接引用的所有內容。

據此,說明如下 :

下面給出一個完整的例子:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace CloneObjectTest
...{

    // Just added to show the difference between shallow and deep cloning
    [Serializable()]
    class SampleRefType
    ...{
        public int Val;
    }

    [Serializable()]
    class Employee
    ...{
        public string m_Name;
        public Int16 m_age;
        public SampleRefType someObject;

        public void Print()
        ...{
            Console.WriteLine("Name : " + m_Name);
            Console.WriteLine("Age : " + m_age);
            Console.WriteLine("Some Value: " + someObject.Val);
        }
        // This is one way to do deep cloning. But works only if the
        // objects and its references are serializable
        public Employee DeepClone()
        ...{
            MemoryStream m = new MemoryStream();
            BinaryFormatter b = new BinaryFormatter();
            b.Serialize(m, this);
            m.Position = 0;
            return (Employee)b.Deserialize(m);
        }
        // Do shallow cloning
        public Employee ShallowClone()
        ...{
            return (Employee)this.MemberwiseClone();
        }
        static void Main(string[] args)
        ...{
            SampleRefType objRef = new SampleRefType();
            objRef.Val = 1000;
            Employee objA = new Employee();
            objA.m_Name = "Manoj";
            objA.m_age = 23;
            objA.someObject = objRef;
            Employee objB = objA.DeepClone();
            Employee objC = objA.ShallowClone();
            //objB is a deep clone. So chages made to objRef would not affect objB's value.
            //But objC is shallow cloned. So, if the objRef is changed, so is the value in
            //ObjC. So objC should print 2000 whereas objB should print 1000
            objRef.Val = 2000;

            //Updating some state
            objC.m_age = 100;
            objB.m_Name = "New Name";

            Console.WriteLine("Original Object:");
            objA.Print();
            Console.WriteLine();
            Console.WriteLine("Shallow Cloned Object:");
            objC.Print();
            Console.WriteLine();
            Console.WriteLine("Deep Cloned Object:");
            objB.Print();
        }
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.