public struct Address { public string Province { get;} public string City { get; } private readonly string[] phones; public string[] Phones { get { string[] rtn = new string[phones.Length]; phones.CopyTo(rtn, 0); return rtn; } } public Address(string province,string city,string[] phones_) { //为字段赋值,使用属性 Province = province; City = city; //为字段赋值,不能使用属性,因为有代码逻辑 phones = new string[phones_.Length]; phones_.CopyTo(phones, 0); } }
不可变类型是线程安全的.
满足的条件
1.外界无法修改成员(无set访问器)
2.构造函数为所有属性赋值