码迷,mamicode.com
首页 > Windows程序 > 详细

effective C#之 - 使用属性代替成员变量

时间:2014-11-24 06:24:15      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   使用   sp   on   div   

  使用属性代替公共成员变量,一个很明显的好处是,很容易在一个地方对成员变量进行控制,例如:

 1 class Customer {
 2         private string name;
 3         public string Name {
 4             set {
 5                 if (string.IsNullOrEmpty(value))
 6                     throw new ArgumentNullException("Name", "Name can not be blank");
 7                 name = value;
 8             }
 9             get { return name; }
10         }
11     }

  反过来说,如果使用公共成员变量,则对于所有出现公共成员变量的地方,都要重复进行相同的处理,这增加了代码的冗余和我们宝贵的时间.

effective C#之 - 使用属性代替成员变量

标签:style   blog   io   ar   color   使用   sp   on   div   

原文地址:http://www.cnblogs.com/listened/p/4117843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!