.net深拷贝与浅拷贝


public class Dog
{
  public string Name{get;set;}
}
public class Person
{
  public string Name{get;set;}
  public int Age{get;set;}
  pulic Dog HisDog{get;set;}
}
  Dog dog1=new Dog();
  dog.Name="Jack";
  Person p1=new Person();
  p1.Name="Tom";
  p1.Age=18;
  p1.Dog=dog1;

//引用,p2与p1指向同一对象,改变任何一个,另一个也会变
  Person p2=p1;

//浅拷贝,Dog指向同一对象
  Person p2=new Person();
  p2.Name=p1.Name;
  p2.Age=p1.Age;
  p2.Dog=p1.Dog;

//深拷贝,共有4个不同对象
  Dog dog2=new Dog();
  dog2.Name=dog1.Name;
  Person p2=new Person();
  p2.Name=p1.Name;
  p2.Age=p1.Age;
  p2.Dog=dog2;