【发布时间】:2011-04-23 11:59:01
【问题描述】:
我正在使用 this.MemberwiseClone() 创建浅拷贝,但它不起作用。请看下面的代码。
public class Customer
{
public int Id;
public string Name;
public Customer CreateShallowCopy()
{
return (Customer)this.MemberwiseClone();
}
}
class Program
{
static void Main(string[] args)
{
Customer objCustomer = new Customer() { Id = 1, Name = "James"};
Customer objCustomer2 = objCustomer;
Customer objCustomerShallowCopy = objCustomer.CreateShallowCopy();
objCustomer.Name = "Jim";
objCustomer.Id = 2;
}
}
当我运行程序时,它显示 objCustomerShallowCopy.Name 为“James”而不是“Jim”。
有什么想法吗??
【问题讨论】:
标签: c# clone shallow-copy