/// <summary>
/// 深拷贝接口
/// </summary>
interface IDeepCopy
{
object DeepCopy();
}
/// <summary>
/// 浅拷贝接口
/// </summary>
interface IShallowCopy
{
object ShallowCopy();
}
class A : IDeepCopy, IShallowCopy
{
struct StructA
{
int b;
};
bool arg0;
StructA arg1;
char[] arg2=new char[8];
//深拷贝
public object DeepCopy()
{
A r = new A();
r.arg0 = this.arg0;
r.arg1 = this.arg1;
r.arg2 = this.arg2;
return r;
}
public object ShallowCopy()
{
//直接使用内置的浅拷贝方法返回
return this.MemberwiseClone();
}
}
class Main
{
public A a = new A();
public A b = new A();
public void Start()
{
b = (A)a.DeepCopy();
}
}
C#类的拷贝
最新推荐文章于 2022-04-09 16:53:17 发布