Shallow copy or Deep copy?

前端 未结 6 877
Happy的楠姐
Happy的楠姐 2020-11-27 04:28

I am a bit new to these two methods of copying one object into the other. I am confused and unable to spot out the major difference between deep copy and shallow copy.. I ha

6条回答
  •  清歌不尽
    2020-11-27 05:12

    It's a shallow copy because if you modify the variable of ob2 - and then try and print ob1 - they will be the same. This is because things in C# that are classes create links between themselves. If you want to do a deep copy, you should implement a copy method and copy the fields by hand. Something like:

      class A
        {
            public int a = 0;
            public void display()
            {
                Console.WriteLine("The value of a is " + a);
            }
    
           public A Copy()
        {
            A a = new A();
            a.a = = this.a;
            return a;
        }
    
    
    
        }
    

提交回复
热议问题