posts - 85,comments - 18,views - 10万

 

正文

前言:日常啪啪啪代码的时候,常常遇到浅复制与深复制的问题,下面就自己经验写写

Clone()

有时候在项目中需要得到一个对象在某个状态下的副本,为了避免重新创建一个对象再一一赋值,便可以使用克隆来处理,克隆分为浅拷贝和深拷贝。

 

浅拷贝

浅拷贝自带方法MemberwiseClone() 对于值类型和String类型成员,浅拷贝会在副本中重新创建成员;对于引用类型,对象和副本对象引用同一个内存地址,当在对象或者副本对象修改引用成员后,引用类型的成员就发生变化。

复制代码
class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person() { Age=12, Name="张三" };
            Person p2 = (Person)p.Clone();
            p2.Name = "李四";
            Console.WriteLine("p.Name="+ p.Name);
            Console.WriteLine("p2.Name=" + p2.Name);
            Console.ReadKey();

        }
    }

    public class Person:System.ICloneable
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
复制代码

输出结果: 

 

深拷贝

深拷贝不管是值类型还是引用类型,对象和副本对象修改成员属性都只会改变各自对象的值,两个对象是完全独立的实体。

1. 第一种实现方式

复制代码
class People
{
    public int _age;
    public string _name;
    public People(int Age,string Name)
    {
        _age = Age;
     _name = Name;
    }

    public object Clone()
    {
        People MySelf = new People(this._age,this._name);
        return MySelf;    }
}
复制代码

2. 第二种实现方式

复制代码
class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person() { 
                Age=12, 
                Name="张三" ,
                Address = new Address()
                {
                    Provice = "上海",
                    City = "上海市"
                }
           };

            //浅拷贝
            Person p2 = (Person)p.Clone();
            p2 .UserName = "浅拷贝用户名";
            p2 .Address.Provice = "浅拷贝上海";
            
           //通过序列化、反序列化实现深拷贝
            Person p3= JsonConvert.DesperializeObject<Person>(JsonConvert.SerializeObject(p));
            p3.UserName = "深拷贝用户名";
            p3.Address.Provice = "深拷贝上海";


        }
    }

    public class Address
    {
        public string Provice { get; set; }
 
        public string City { get; set; }
    }

            

    public class Person:System.ICloneable
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
             
复制代码

 



posted on   JamelAr  阅读(9388)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

打赏

>>

欢迎打赏支持我 ^_^

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示