这很有用,但我不确定为什么它有效
protected override Boolean IsValid(String propertyValue)
{
return !String.IsNullOrEmpty(propertyValue) && propertyValue.Trim().Length > 0;
}
这个 C# 验证方法完全符合我的要求,但我不知道您可以像这样使用表达式短路。
当 propertyValue 为 null 时,执行是否仍然需要计算表达式的第二部分以产生布尔结果,如果是这样,为什么第二部分不为 Trim() 抛出 null 引用异常。长度> 0 电话?
我假设第二部分的计算结果为 false 或 null,但我很想知道这里发生了什么。
抱歉,如果我遗漏了一些非常明显的东西,请启发我。
上帝 - 我错过了一些明显的白痴! - 完全空白,因为当第一部分是错误的时,第二部分是无关紧要的,我什至知道它是短暂的 - 抱歉浪费了人们的时间时间,尴尬的表情是什么?
添加了另一个标签来反映我在思考之前打字的尴尬。
protected override Boolean IsValid(String propertyValue)
{
return !String.IsNullOrEmpty(propertyValue) && propertyValue.Trim().Length > 0;
}
This C# validation method does exactly what I want, but I wasn't aware that you could use expression short-circuiting like this.
When propertyValue is null, doesn't execution still need to evaluate the second part of the expression to yield a boolean result, and if so why doesn't the second part then throw a null ref exception for the Trim().Length > 0 call?
I assume that the second part is evaluating to false or to null, but I am curious to know what is going on here.
Apologies if I'm missing something really obvious, please enlighten me.
God - I was missing something obvious what an idiot! - total blank on the fact that when the first part is false the second part is irrelevant and I even KNEW it was short-ciruciting - sorry for wasting people's time, what's the emoticon for embarassment?
Added another tag to reflect my embarrassment at typing before thinking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是短路的作用。当
e1 的计算结果为 false 时,e2 根本不会被计算。
(
如果 e1 为 true,则根本不会计算 e2。)
在 e2 具有“效果”(执行 I/O、抛出异常、... )因为短路会产生与完整评估不同的程序语义。
This is what short-circuiting does. When you have
and e1 evaluates to false, e2 is not evaluated at all.
(And for
if e1 is true, e2 is not evaluated at all.)
This is important to understand in the case where e2 has "effects" (does I/O, throws an exception, ...) as short-circuiting yields different program semantics than full evaluation would.
为了使合取为真,它的两个组成部分都必须为真。由于这个定义,并且因为 C# 首先评估左连接,如果左连接评估为 false,则答案已经为 false,并且 C# 不需要评估右连接。
In order for a conjunction to be true, both of its components need to be true. Due to this definition, and because C# evaluates the left conjunct first, if the left conjunct evaluates to false, then the answer is already false, and C# does not need to evaluate the right conjunct.
这是因为 &&操作员
来自 MSDN
是一个值得了解的好功能关于:)
如果你有
如果A是假的,A和B将总是评估为假,所以B是什么并不重要。
This is because of the && operator
from MSDN
Is a nice feature to know about :)
If you have
if A is false, A and B will always evaluate to false, so it doesnt really matter what B is.
这就是短路的意思。当第一个操作数产生 false 时,不需要计算第二部分,因为无论如何结果都是 false。
This is what short-circuit means. When the first operand yields false, the second part does not need to be evaluated, because the result will be false anyways.
有一个添加(&&)。因此,当第一部分为假时,不需要评估第二部分。
这比旧的 VB 6 天要好得多,当时该语句会导致空引用异常。
There is an add (&&). So when the first part is false it does not need to evaluate the second part.
This is much better than the old VB 6 days, when that statement would have resulted in a null reference exception.