什么时候和为什么我应该使用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) {}
我知道我可以使用它, code> +1 重载ctor,但我不能理解以这种方式定义无参构造函数
的优点。
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屋!
查看全文