base() and this() constructors best practices

后端 未结 5 1729
予麋鹿
予麋鹿 2020-12-02 05:47

Under what conditions am I supposed to make the :base() and :this() constructor calls following my constructor\'s parentheses (or even in other pl

5条回答
  •  既然无缘
    2020-12-02 06:45

    Look for "constructor chaining in C#". Basically, it looks like this:

    MyClass():base()  //default constructor calling superclass constructor
    {
    }
    
    MyClass(int arg):this()  //non-basic constructor calling base constructor
    {
        //extra initialization
    }
    

    It helps to remove code duplication in constructors - split them into basic and specific parts.

提交回复
热议问题