也许会有人这样解释C# 中浅拷贝与深拷贝区别:
浅拷贝是对引用类型拷贝地址,对值类型直接进行拷贝。
不能说它完全错误,但至少还不够严谨。比如:string 类型咋说?
其实,我们可以通过实践来寻找答案。
首先,定义以下类型:
int 、string 、enum 、struct 、class 、int[ ] 、string[ ]
代码如下:
| //枚举 | 
| public enum myEnum | 
| { _1 = 1, _2 = 2 } | 
| //结构体 | 
| public struct myStruct | 
| { | 
| public int _int; | 
| public myStruct(int i) | 
| { _int = i; } | 
| } | 
| //类 | 
| class myClass | 
| { | 
| public string _string; | 
| public myClass(string s) | 
| { _string = s; } | 
| } | 
| //ICloneable:创建作为当前实例副本的新对象。 | 
| class DemoClass : ICloneable | 
| { | 
| public int _int = 1; | 
| public string _string = "1"; | 
| public myEnum _enum = myEnum._1; | 
| public myStruct _struct = new myStruct(1); | 
| public myClass _class = new myClass("1"); | 
| //数组 | 
| public int[] arrInt = new int[] { 1 }; | 
| public string[] arrString = new string[] { "1" }; | 
| //返回此实例副本的新对象 | 
| public object Clone() | 
| { | 
| //MemberwiseClone:返回当前对象的浅表副本(它是Object对象的基方法) | 
| return this.MemberwiseClone(); | 
| } | 
| } | 
注意:
ICloneable 接口:支持克隆,即用与现有实例相同的值创建类的新实例。
MemberwiseClone 方法:创建当前 System.Object 的浅表副本。
接下来,构建实例A ,并对实例A 克隆产生一个实例B。
 然后,改变实例B 的值,并观察实例A 的值会不会被改变。
代码如下:
| class 浅拷贝与深拷贝 | 
| { | 
| static void Main(string[] args) | 
| { | 
| DemoClass A = new DemoClass(); | 
| //创建实例A的副本 --> 新对象实例B | 
| DemoClass B = (DemoClass)A.Clone(); | 
| B._int = 2; | 
| Console.WriteLine(" int \t\t A:{0} B:{1}", A._int, B._int); | 
| B._string = "2"; | 
| Console.WriteLine(" string \t A:{0} B:{1}", A._string, B._string); | 
| B._enum = myEnum._2; | 
| Console.WriteLine(" enum \t\t A:{0} B:{1}", (int)A._enum, (int)B._enum); | 
| B._struct._int = 2; | 
| Console.WriteLine(" struct \t  A:{0}  B:{1}", A._struct._int, B._struct._int); | 
| B._class._string = "2"; | 
| Console.WriteLine(" class \t\t  A:{0}  B:{1}", A._class._string, B._class._string); | 
| B.arrInt[0] = 2; | 
| Console.WriteLine(" intArray \t  A:{0}  B:{1}", A.arrInt[0], B.arrInt[0]); | 
| B.arrString[0] = "2"; | 
| Console.WriteLine(" stringArray \t  A:{0}  B:{1}", A.arrString[0], B.arrString[0]); | 
| Console.ReadKey(); | 
| } | 
| } | 
结果如下:
 
 
从最后的输出结果,我们得知:
对于内部的Class 对象和数组,则Copy 一份地址。[ 改变B 时,A也被改变了 ]
而对于其它内置的int / string / enum / struct / object 类型,则Copy 一份值。
有一位网友说:string 类型虽然是引用类型,但是很多情况下.Net 把string 做值类型来处理,我觉得string 应该也是按照值类型处理的。
这说明他对string 类型还不够了解。
可以肯定的是:string 一定是引用类型。那它为什么是深拷贝呢?
如果你看一下string 类型的源代码就知道了:
| //表示空字符串。此字段为只读。 | 
| public static readonly string Empty; | 
答案就在于 string 是 readonly 的,当改变 string 类型的数据值时,将重新分配了内存地址。
下面引用一段网友的代码:Vseen[ Aloner ] 的个人陋见:
| public class Student | 
| { | 
| // 这里用“字段”,其实应当是属性。 | 
| public string Name; | 
| public int Age; | 
| //自定义类 Classroom | 
| public Classroom Class; | 
| } | 
| 浅拷贝:Student A 浅拷贝出 Student B,Name和Age拥有新的内存地址,但引用了同一个 Classroom。 | 
| 深拷贝:Student A 浅拷贝出 Student B,Name和Age拥有新的内存地址,并且A.Classroom 的内存地址不等于 B.Classroom。 | 
| 其实俗点讲,有点像: | 
| public object Clone() | 
| { | 
| Student B = new Student(); | 
| B.Name = this.Name; | 
| B.Age = this.Age; | 
| //浅拷贝 | 
| B.Class = this.Class; | 
| //深拷贝 | 
| B.Class = new Classromm(); | 
| B.Class.Name = this.Class.Name; | 
| B.Class.Teacher = this.Class.Teacher; //根据情况,对Teacher 进行判定要进行的是深拷贝,还是浅拷贝。 | 
| } | 
浅拷贝:给对象拷贝一份新的对象。
浅拷贝的定义 —— 只对值类型(或string)类型分配新的内存地址。
深拷贝:给对象拷贝一份全新的对象。
深拷贝的定义 —— 对值类型分配新的内存地址,引用类型、以及引用类型的内部字段分配的新的地址。
我是这么定义的:浅拷贝,换汤不换药。
注意:
1、在 .NET 程序中,应该避免使用 ICloneable 接口。
因为通过该接口无法判断究竟是浅拷贝还是深拷贝,这会造成误解或误用。
2、深拷贝应该复制该对象本身及通过该对象所能到达的完整的对象图,浅拷贝只复制对象本身(就是该对象所表示的在堆中的一块连续地址中的内容)。
个人愚见:
Clone :深层拷贝,拷贝到了指针指向的内存块的值。
浅拷贝:仅仅拷贝了指针的内容。(只是给一个对象多起了个名字,所以,当改变拷贝的某个属性的时候,原对象的对应属性亦会改变)。
C# 读书笔记 ---- 浅度复制与深度复制
对值类型字段只是简单的拷贝一个副本到目标对象,改变目标对象中值类型字段的值不会反映到原始对象中,因为拷贝的是副本;
对引用型字段则是指拷贝他的一个引用到目标对象。改变目标对象中引用类型字段的值它将反映到原始对象中,因为拷贝的是指向堆是上的一个地址;
深拷贝:深拷贝与浅拷贝不同的是对于引用字段的处理,深拷贝将会在新对象中创建一个新的对象和原始对象中对应字段相同(内容相同)的字段,也就是说这个引用和原始对象的引用是不同, 我们改变新对象中这个字段的时候是不会影响到原始对象中对应字段的内容。
推荐: