也许会有人这样解释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(); |
} |
}
对于C#
深拷贝和浅拷贝的区别表现在引用类型,对于引用类型,浅拷贝的原对象变化,拷贝后的对象也会变化,深拷贝则原对象变化,拷贝后的对象不会变化
|