【发布时间】:2012-02-27 04:03:49
【问题描述】:
鉴于典型的编码准则是“不要在方法调用中引起副作用”。并且不使用短路运算符的唯一原因(我知道 - 如果我错了请赐教)是当您依赖后续代码中方法调用的副作用时。为什么 C# 和 VB.NET 等语言中的默认运算符不是短路版本?
IE:
if (Method1() & Method2()) {
}
if Method1 And Method2 then
End if
if (Method1() | Method2()) {
}
if Method1 Or Method2 then
End if
实际上(默认情况下)意味着
if (Method1() && Method2()) {
}
if Method1 AndAlso Method2 then
End if
if (Method1() || Method2()) {
}
if Method1 OrElse Method2 then
End if
【问题讨论】:
-
C# 确实使用短路评估。也就是说,这个问题看起来像 stackoverflow.com/questions/1445867/…
标签: c# vb.net language-agnostic short-circuiting