何时以及为什么应该使用 ClassName : this(null)? [英] When and why should I use ClassName : this(null)?
本文介绍了何时以及为什么应该使用 ClassName : this(null)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法理解一个简单的裸
i can't understand the difference between a simple bare
Public ClassName() {}
和
Public ClassName() : this(null) {}
我知道只有当我有一个 +1
重载的构造函数时我才能使用它,但是我无法理解以这种方式定义无参数构造函数
的优点.
I know I can use it only if i have a +1
overloaded ctor, but I can't understand the advantages of defining the parameterless constructor
this way.
推荐答案
这允许单参数构造函数拥有所有逻辑,因此不再重复.
This permits the single-param constructor to have all the logic, so it isn't repeated.
public ClassName() : this(null) {}
public ClassName(string s)
{
// logic (code)
if (s != null) {
// more logic
}
// Even more logic
}
我希望很清楚,如果不是 this(null)
,逻辑"和更多逻辑"将需要在无参数构造函数中重复.
I hope it's clear that the "logic" and "even more logic" would have needed to be repeated in the parameterless constructor if not for the this(null)
.
这篇关于何时以及为什么应该使用 ClassName : this(null)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文