现在的位置: 首页 > 综合 > 正文

浅尝EffectiveCSharp_1

2013年04月30日 ⁄ 综合 ⁄ 共 3503字 ⁄ 字号 评论关闭

Item 1: 使用属性,避免可访问的数据成员  Use Properties Instead of Accessible Data Members

 

  • 属性允许你创建一个想可访问数据的接口,而且仍然有使用方法的所有优点.Properties enable you to create an interface that acts like data access but still has all the benefits of a method.在.NET框架设计中数据绑定支持属性,而不是公共数据成员,在WPF,WF,WebForms,Silverlight中是一样的.In fact, the data binding classes in the .NET Framework support properties, not public data members. This is true for all the data binding libraries: WPF, Windows Forms, Web Forms, and Silverlight.
  • textBoxCity.DataBindings.Add("Text",address, "City");
  • 属性更易于更改,可以做校验或者额外的验证Properties are far easier to change as you discover new requirements or behaviors over time. You might soon decide that your customer type should never have a blank name. If you used a public property for Name, that’s easy to fix in one location
    1 public class Customer
    2 {
    3 private string name;
    4 public string Name
    5 {
    6 get { return name; }
    7 set
    8 {
    9 if (string.IsNullOrEmpty(value))
    10 throw new ArgumentException( "Name cannot be blank", "Name");
    11 name = value;
    12 }
    13 }
    14 }
  • 因为属性里可以使用方法,因此添加多线程变得更加容易.Because properties are implemented with methods, adding multithreaded support is easier. You can enhance the implementation of the get and set accessors to provide synchronized access to the data:
    1 public class Customer
    2 {
    3 private object syncHandle = new object();
    4 private string name;
    5 public string Name
    6 {
    7 get
    8 {
    9 lock (syncHandle)
    10 return name;
    11 }
    12 set
    13 {
    14 if (string.IsNullOrEmpty(value))
    15 throw new ArgumentException("Name cannot be blank","Name");
    16 lock (syncHandle)
    17 name = value;
    18 }
    19 }
    20 }
  • 属性也可以是虚的,或者是抽象的.Properties have all the language features of methods. Properties can be virtual:
    1 public class Customer
    2 {
    3 public virtual string Name { get; set; }
    4 }

    这是C#3.0隐式声明语法.

  • 你可以对get和set两个属性访问器做任何的修改,这比单单定义数据可见性强大的多.You can specify different accessibility modifiers to the get and set accessors in a property in C#. This gives you even greater control over the visibility of those data elements you expose as properties,这里也可以提供get-only或者set-only版本,甚至可以给读、写以不同的访问权限
    1 public class Customer
    2 {
    3 public virtual string Name
    4 {
    5 get;
    6 protected set;
    7 }
    8 }

     

Item 2: 使用只读方式,避免使用常量声明 Prefer readonly to const

  • C#有两种常量版本,一种是编译时常量,另一种是运行时常量.错误的运用会导致性能和正确性降低.C# has two different versions of constants: compile-time constants and runtime constants. They have very different behaviors, and using the wrong one will cost you performance or correctness. 编译时常量比运行时常量稍微快点,但是灵活性差很多Compile-time constants are slightly faster, but far less flexible, than runtime constants
  • 用readonly声明运行时常量,const声明编译时常量You declare runtime constants with the readonly keyword. Compile-time constants are declared with the const keyword.
  • 编译时常量(const)可以在方法声明,运行时常量(readonly)不能在方法范围内声明Compile-time constants can also be declared inside methods. Read-only constants cannot be declared with method scope.
  • 编译时常量被限制为数字,字符串类型.而运行时常量则可以是任何类型Compile-time constants are limited to numbers and strings. Read-only values are also constants, in that they cannot be modified after the constructor has executed. But read-only values are different in that they are assigned at runtime. You have much more flexibility in working with runtime constants. For one thing, runtime constants can be any type.
  • 编译时常量与运行时常量行为的不同处在于它们的访问方式。编译时常量在编译后的结果代码中会被替换为该常量的值,例如下面的代码The differences in the behavior of compile-time and runtime constants follow from how they are accessed. A compile-time constant is replaced with the value of that constant in your object code. This construct:

    if (myDateTime.Year == Millennium)

     

     compiles to the same IL as if you had written this:

    if (myDateTime.Year == 2000)

     

 

If you’re still creating public variables in your
types, stop now. If you’re still creating get and set methods by hand, stop
now. Properties let you expose data members as part of your public interface
and still provide the encapsulation you want in an object-oriented
environment. Properties are language elements that are accessed as though
they are data members, but they are implemented as methods.

抱歉!评论已关闭.