c# 深拷貝 淺拷貝

來源:互聯網
上載者:User

標籤:

  淺拷貝對參考型別只拷貝地址,拷貝前後共用一塊記憶體地區。深拷貝就是所有的東西全部重新有一份,沒有共用存在,推薦使用序列化深拷貝。

using System;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace ConsoleApplication1{    internal class Program    {        [Serializable]        public class TV        {            public TV()            {                            }            public TV(int max)            {                MaxChannel = max;            }            private int maxChannel;            public override string ToString()            {                return string.Format("MaxChannel: {0}", MaxChannel);            }            public int MaxChannel            {                get { return maxChannel; }                set { maxChannel = value; }            }        }        [Serializable]        public struct Light        {            public string name;            public Light(string name)            {                this.name = name;            }            public override string ToString()            {                return string.Format("Name: {0}", name);            }        }        [Serializable]        public class House : ICloneable        {            public TV tv;            public Light light;            public House(TV tv, Light light)            {                this.tv = tv;                this.light = light;            }            public override string ToString()            {                return string.Format("Light: {0}, Tv: {1}", light, tv);            }            public object Clone()            {                //淺拷貝                //return MemberwiseClone();                //深拷貝 1                //TV tv = new TV();                //tv.MaxChannel = this.tv.MaxChannel;                //Light light = this.light;                //House house = new House(tv, light);                //return house;                //深拷貝2 需要添加[Serializable]                using (Stream stream = new MemoryStream())                {                    BinaryFormatter bf = new BinaryFormatter();                    bf.Serialize(stream, this);                    stream.Seek(0, SeekOrigin.Begin);                    return bf.Deserialize(stream);                }            }        }            private static void Main(string[] args)            {                TV tv = new TV(10);                Light light = new Light("zk");                House house = new House(tv, light);                House house1 = (House)house.Clone();                Console.WriteLine(house);                Console.WriteLine(house1);                house.light.name = "te";                house.tv.MaxChannel = 90;                Console.WriteLine(house);                Console.WriteLine(house1);            }    }}

 

c# 深拷貝 淺拷貝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.