【发布时间】:2012-03-28 15:12:57
【问题描述】:
我认为这部分与短路逻辑有关,但我找不到任何直接回答我问题的问题。可能的相关问题:Benefits of using short-circuit evaluation、Why use short-circuit code?
考虑以下两个代码块,它们都是类的可能构造函数
public MyClass(OtherClass other){
if (other != null) {
//do something with other, possibly default values in this object
}
}
还有这个
public MyClass(OtherClass other){
if (other == null) return;
//do something with other, possibly default values in this object
}
做后者比做前者有什么好处吗?构造函数中没有其他代码,只是使用other 对象来构造这个的代码。
【问题讨论】:
标签: c# constructor short-circuiting