浅拷贝 −
浅拷贝是指拷贝一个对象的"主要"部分,但不拷贝内部的部分 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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号