浅拷贝−
浅拷贝是指拷贝一个对象的"主要"部分,但不拷贝内部的部分 objects.
The "inner objects" are shared between the original object and its copy.
The problem with the shallow copy is that the two objects are not independent. If you 修改一个对象,改变将会反映在另一个对象中。
深拷贝−
深拷贝是一个完全独立的对象副本。如果我们复制了我们的对象, would copy the entire object structure.
If you modify the one object, the change will not be reflected in the other object.
class Program{ static void Main(string[] args){ //Shallow Copy ShallowCopy obj = new ShallowCopy(); obj.a = 10; ShallowCopy obj1 = new ShallowCopy(); obj1 = obj; Console.WriteLine("{0} {1}", obj1.a, obj.a); // 10,10 obj1.a = 5; Console.WriteLine("{0} {1}", obj1.a, obj.a); //5,5 //Deep Copy DeepCopy d = new DeepCopy(); d.a = 10; DeepCopy d1 = new DeepCopy(); d1.a = d.a; Console.WriteLine("{0} {1}", d1.a, d.a); // 10,10 d1.a = 5; Console.WriteLine("{0} {1}", d1.a, d.a); //5,10 Console.ReadLine(); } } class ShallowCopy{ public int a = 10; } class DeepCopy{ public int a = 10; }
10 10 5 5 10 10 5 10
以上是什么是浅复制以及它与 C# 中的深复制有何不同?的详细内容。更多信息请关注PHP中文网其他相关文章!