我何时以及为什么要使用ClassName:this(null)?

Ser*_*gio 8 c# parameters constructor

我无法理解简单裸露之间的区别

Public ClassName() {}
Run Code Online (Sandbox Code Playgroud)

Public ClassName() : this(null) {}
Run Code Online (Sandbox Code Playgroud)

我知道只有当我有一个+1超载的ctor 我才能使用它,但我无法理解defining the parameterless constructor这种方式的优点.

Joh*_*ers 10

这允许单参数构造函数具有所有逻辑,因此不会重复.

public ClassName() : this(null) {}

public ClassName(string s)
{
    // logic (code)
    if (s != null) {
        // more logic
    }
    // Even more logic
}
Run Code Online (Sandbox Code Playgroud)

我希望很明显,如果没有参数,那么在无参数构造函数中需要重复"逻辑"和"更多逻辑" this(null).