我正在使用 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”。
有任何想法吗??