C# 物件的淺複製、深複製

2018/06/26 17:15 東興 C#

public class Person {

    public int Age { get; set; }

    public string Address { get; set; }

    public Name Name { get; set; }

    public List<string> Phone { get; set; } = new List<string>();

    public Person Clone() {

        var person = new Person();

        person.Age = this.Age;

        person.Address = this.Address;

        person.Name = this.Name;

        var phone = new List<string>();

        foreach (var item in this.Phone)

        {

            phone.Add(item);

        }

        person.Phone = phone;

         return person;

    }

}

 

以上Clone()為深複製方式,詳情可參考連結

https://dotblogs.com.tw/yc421206/archive/2011/06/17/28785.aspx 

發表評論

  • (600字以內)
留言身份 :

此篇評論