zoukankan      html  css  js  c++  java
  • 编写高质量代码改善C#程序的157个建议——建议128:考虑让派生类的名字以基类名字作为后缀

    建议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个建议》陆敏技

  • 相关阅读:
    Understanding Paxos Algorithm
    Distributed Transaction, 2-Phase Commit, 3-Phase Commit
    Mesos Resource Allocation Algo: DRF(Dominant Resource Fairness)
    AWS Whitepaper
    HackerRank
    Spark Tutorial recommended
    [Checked (vid only)] Cousera
    LeetCode 853. Car Fleet
    [Checked] Introduction to Linear Algebra
    [Checked] Udacity
  • 原文地址:https://www.cnblogs.com/jesselzj/p/4752357.html
Copyright © 2011-2022 走看看