Deep Copy in C#

后端 未结 8 627
独厮守ぢ
独厮守ぢ 2021-02-05 20:36

MSDN gives this example of a deep copy (http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx)

public class Person 
{
    public int Age;
           


        
8条回答
  •  不知归路
    2021-02-05 21:22

    The implementation of MemberwiseClone would do the following for your code.

    Person p = new Person();
    p.Age = this.Age;  // value copy
    p.Name = this.Name; // value copy
    p.IdInfo = this.IdInfo; // reference copy. this object is the same in both coppies.
    return p;
    

提交回复
热议问题