Tho*_*nes 4 c# constructor short-circuiting
我认为这部分与短路逻辑有关,但我找不到任何直接回答我问题的问题.可能的相关问题:使用短路评估的好处,为什么要使用短路代码?
考虑以下两个代码块,这两个代码块都是类的可能构造函数
public MyClass(OtherClass other){
if (other != null) {
//do something with other, possibly default values in this object
}
}
Run Code Online (Sandbox Code Playgroud)
还有这个
public MyClass(OtherClass other){
if (other == null) return;
//do something with other, possibly default values in this object
}
Run Code Online (Sandbox Code Playgroud)
对前者做后者有什么好处?构造函数中没有其他代码,只是使用该other对象构造此代码的代码.
| 归档时间: |
|
| 查看次数: |
126 次 |
| 最近记录: |