建議128:考慮讓派生類的名字以基類名字作爲後綴
派生類的名字可以考慮以基類名字作爲後綴。這帶來的好處是,從類型的名字上我們就知道它包含在哪一個繼承體系中。
Exception及其子類就是這樣一個典型的例子。所有的異常都應該繼承自System.Exception,而所有的異常都應該命名爲CustomedException。如果在VS中輸入Exception,再按Tab鍵,會自動生成如下代碼:
[Serializable] public class MyException : Exception { // // For guidelines regarding the creation of new exception types, see // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp // and // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp // public MyException() { } public MyException(string message) : base(message) { } public MyException(string message, Exception inner) : base(message, inner) { } protected MyException( SerializationInfo info, StreamingContext context) : base(info, context) { } }
從這裏我們可以看出,微軟支持讓派生類的名字以基類名字作爲後綴。
在FCL中,這類常用的例子還有Attribute、EventArgs等。
轉自:《編寫高質量代碼改善C#程序的157個建議》陸敏技