Immutable type

   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;
             // for the field assignment, attributes can not be used, because the code logic 
            PHONES = new new String [phones_.Length]; 
            phones_.CopyTo (PHONES, 0); 
        } 
    }

Immutable type is thread safe.

Conditions are met

1. The members of the outside world can not be modified (no set accessor)

2. The constructor for all property assignment

Guess you like

Origin www.cnblogs.com/zhenguan/p/11310177.html