浅尝EffectiveCSharp_1
生活随笔
收集整理的這篇文章主要介紹了
浅尝EffectiveCSharp_1
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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.轉載于:https://www.cnblogs.com/TivonStone/archive/2010/04/24/1719566.html
總結
以上是生活随笔為你收集整理的浅尝EffectiveCSharp_1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VMware15安装mac10.14
- 下一篇: 札记__ADT:URL,Lua:strl