程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> IDesign C#編碼規范(之三)

IDesign C#編碼規范(之三)

編輯:關於C語言
續之二,IDesign C#編碼規范之三。

34. 避免使用new繼承修飾符,而是使用override。
Avoid using the new inheritance qualifIEr. Use override instead.
35. 對非密封類總是將public和protected方法標記為virtual。
Always mark public and protected methods as virtual in a non sealed class.
36. 除非涉及到互操作,永遠不要用不安全的代碼。
Never use unsafe code unless when using interop.
37. 避免顯式類型轉換。使用as算法防護性地轉換類型。
Avoid explicit casting. Use the as Operator to defensively cast to a type.
Dog dog = new GermanShepherd();
GermanShepherd shepherd = dog as GermanShepherd;
if(shepherd != null)
{...}
38. 類成員有委托時:
With delegates as class members:
a) 使用前將委托復制到局部變量,以避免並發沖突。
Copy a delegate to a local variable before publishing to avoid concurrency race condition.
b) 調用前始終檢查委托是否為空。
Always check a delegate for null before invoking it.
public class MySource
{
public event EventHandler MyEvent;
public void FireEvent()
{
EventHandler temp = MyEvent;
if(temp != null)
{
temp(this,EventArgs.Empty);
}
}
}
39. 不要提供public的事件成員變量,而是使用事件訪問器。
Do not provide public event member variables. Use event Accessors instead.
public class MySource
{
MyDelegate m_SomeEvent;
public event MyDelegate SomeEvent
{
add
{
m_SomeEvent += value;
}
remove
{
m_SomeEvent -= value;
}
}
}
40. 使用Programming .Net Components中定義的EventsHelper類安全地發布事件。
Use the EventsHelper class defined in Programming .Net Components to publish events defensively.
41. 總是使用接口。
Always use interfaces.
a) 參見Programming .Net Components第一和第三章。
See Chapters 1 and 3 in Programming .Net Components.
42. 類和接口中方法和屬性的比例至少是2:1。
Classes and interfaces should have at least 2:1 ratio of methods to propertIEs.
43. 避免使用一個成員的接口。
Avoid interfaces with one member.
44. 努力使每個接口擁有3-5個成員。
Strive to have 3-5 members per interface.
45. 每個接口不用超過20個成員。
No more than 20 members per interface.
a) 12可能是實際應用的極限了。
12 is probably a practical limit.
46. 避免將事件作為接口成員。
Avoid events as interface members.
47. 避免使用抽象方法,而是使用接口代替。
Avoid abstract methods, use interfaces instead.
48. 在類層次中暴露接口。
Expose interfaces on class hierarchIEs.
a) 參見Programming .Net Components第三章。
See Chapter 3 in Programming .Net Components.
49. 優先使用明確的接口實現。
Prefer using explicit interface implementation.
a) 參見Programming .Net Components第三章。
See Chapter 3 in Programming .Net Components.
50. 永遠不要假設一種類型支持某個接口。防護性地檢查是否支持該接口。
Never assume a type supports an interface. Defensively query for that interface.
SomeType obj1;
IMyInterface obj2;

/* Some code to initialize obj1, then: */
obj2 = obj1 as IMyInterface;
if(obj2 != null)
{
obj2.Method1();
}
else
{
//Handle error in expected interface
}
51. 將呈現給用戶的字符串永遠不用硬編碼,而是使用資源。
Never hardcode strings that will be presented to end users. Use resources instead.
52. 發布時可能修改的字符串永遠不用硬編碼,例如連接字符串。
Never hardcode strings that might change based on deployment such as connection strings.
53. 構建一個長字符串時,使用StringBuilder,不要用string。
When building a long string, use StringBuilder, not string.
54. 避免提供帶結構的方法。
Avoid providing methods on structures.
a) 參數化的構造函數是鼓勵使用的。
Parameterized constructors are encouraged.
b) 可以重載算符。
Can overload Operators.
55. 當提供靜態成員變量時,總是提供一個靜態構造函數。
Always provide a static constructor when providing static member variables.
56. 只要可以用前期綁定就不要用後期綁定。
Do not use late-binding invocation when early-binding is possible.
57. 對應用程序進行日志和跟蹤。
Use application logging and tracing.
58. 除非在switch語句中跳轉,永遠不要用goto語句。
Never use goto unless in a switch statement fall-through.
59. switch語句中總是使用default用於加斷言。
Always have a default case in a switch statement that asserts .
int number = SomeMethod();
switch(number)
{
case 1:
Trace.WriteLine("Case 1:");
break;
case 2:
Trace.WriteLine("Case 2:");
break;
default:
Debug.Assert(false);
break;
}
60. 除非在構造函數中調用另一個構造函數,否則不用使用this。
Do not use the this reference unless invoking another constructor from within a constructor.
//Example of proper use of this
public class MyClass
{
public MyClass(string message)
{}
public MyClass() : this("hello")
{}
}
61. 除非為了解決調用基類構造函數時成員名的沖突,否則不要使用base訪問基類的成員。
Do not use the base Word to Access base class members unless you wish to resolve a conflict with a subclasses member of the same name or when invoking a base class constructor.
//Example of proper use of 抌ase?
public class Dog
{
public Dog(string name)
{}
virtual public void Bark(int howLong)
{}
}
public class GermanShepherd : Dog
{
public GermanShepherd(string name): base(name)
{}
override public void Bark(int howLong)
{
base.Bark(howLong);
}
}
62. 根據Programming .Net Components第四章中的模板實現Dispose()和Finalize()方法。
Implement Dispose() and Finalize() methods based on the template in Chapter 4 of Programming .Net Components.
63. 使用泛型的代碼中避免與System.Object進行類型轉換,而是使用限制或as算符。
Avoid casting to and from System.Object in code that uses generics. Use constraints or the as Operator instead:
class SomeClass
{}
//避免 Avoid:
class MyClass
{
void SomeMethod(T t)
{
object temp = t;
SomeClass obj = (SomeClass)temp;
}
}
//正確 Correct:
class MyClass where T : SomeClass
{
void SomeMethod(T t)
{
SomeClass obj = t;
}
}
64. 泛型接口不要定義限制。接口層的限制通常能用強類型代替。
Do not define constraints in generic interfaces. Interface level-constraint can often be replaced by strong-typing.
public class Customer
{...}
//避免 Avoid:
public interface IList where T : Customer
{...}
//正確 Correct:
public interface ICustomerList : IList
{...}
65. 不要在接口中定義與方法相關的限制。
Do not define method-specific constraints in interfaces.
66. 在數據結構中總是優先使用C#泛型。
Always prefer using C# generics in data structures.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved