C# 如果第一个条件失败,IF 语句会停止评估吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11358576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Will an IF statement stop evaluating if it fails the first condition?
提问by
If I have an Ifstatement with 2 conditions - and the first fails, will the 2nd condition even be considered or will it go straight to the else? So, in the following example, if myList.Count == 0, will myStringbe compared against "value" or will it just straight to else?
如果我有一个If带有 2 个条件的语句 - 第一个失败,第二个条件甚至会被考虑还是直接进入else?因此,在下面的示例中,如果myList.Count == 0, 将myString与“值”进行比较还是直接与else?
if(myList.Count > 0 && myString.Equals("value"))
{
//Do something
}
else
{
//Do something else
}
采纳答案by Paul Spangle
It will stop evaluating because you're using the double ampersand && operator. This is called short-circuiting.
它将停止评估,因为您正在使用双与号 && 运算符。这称为短路。
If you changed it to a single ampersand:
如果您将其更改为单个 & 符号:
if(myList.Count > 0 & myString.Equals("value"))
it would evaluate both.
它会评估两者。
回答by MBen
No, it will not be considered. (This is known as short circuiting.)
不,它不会被考虑。(这被称为短路。)
The compiler is clever enough (and required by language specification) to know that if the first condition is false, there is no way to expression will evaluate to true.
编译器足够聪明(并且是语言规范所要求的)知道如果第一个条件是false,则表达式无法计算为true。
And as Jacob pointed for ||, when the first condition is true, the second condition will not be evaluated.
正如雅各布所指出的||,当第一个条件为 时true,将不会评估第二个条件。
回答by Jigar Joshi
No, second condition will be skipped if you use &&,
不,如果您使用&&,将跳过第二个条件,
If you use &it will be considered
如果你使用&它会被考虑
回答by dtsg
In your example, the second statement will only be evaluated if the first fails. The logical AND &&will only return truewhen both operands are true, aka short circuit evaluation.
在您的示例中,仅当第一个失败时才会评估第二个语句。逻辑 AND&&只会true在两个操作数都为真时返回,也就是短路评估。
回答by Rutesh Makhijani
If the logical operator is AND (&&) then IF statement would evaluate first expression - if the first one is false, it would not evaluate second one. This is useful to check if variable is null before calling method on the reference - to avoid null pointer exception
如果逻辑运算符是 AND (&&),则 IF 语句将评估第一个表达式 - 如果第一个表达式为假,则不会评估第二个表达式。这对于在引用上调用方法之前检查变量是否为空很有用 - 以避免空指针异常
If the logical operator is OR (||) then IF statement would evaluate first expression - if the first one is true, it would not evaluate second one.
如果逻辑运算符是 OR (||),则 IF 语句将评估第一个表达式 - 如果第一个表达式为真,则不会评估第二个表达式。
Compilers and runtimes are optimized for this behavior
编译器和运行时针对此行为进行了优化
回答by Akash KC
.NET supports short circuitingso when first condition goes fail, it will not check the second condtion....In C# || and && are the short-circuited versions of the logical operators | and & respectively....It is often faster too...
.NET 支持,short circuiting所以当第一个条件失败时,它不会检查第二个条件......在 C# 中 || 和 && 是逻辑运算符的短路版本 | 和 & 分别......通常也更快......
回答by BugFinder
Consider the folowing:
考虑以下:
static int? x;
static int? y;
static void Main(string[] args)
{
x = 5;
if (testx() & testy())
{
Console.WriteLine("test");
}
}
static Boolean testx()
{
return x == 3;
}
static Boolean testy()
{
return y == 10;
}
If you trace through both the testx and testy functions are evaulated even though testx was false.
如果您跟踪 testx 和 testy 函数,即使 testx 是假的也会被评估。
If you change the test to && only the first was checked.
如果将测试更改为 && 则仅检查第一个。
回答by Travis J
The expression will be evaluated from left to right because it is a logical AND operation. If false, it will stop evaluation. It also has the greatest precedence of the operations in your expression.
表达式将从左到右计算,因为它是逻辑 AND 运算。如果为假,它将停止评估。它还具有表达式中操作的最大优先级。
The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary. Conditional And Operator (&&) MSDN
条件与运算符 (&&) 执行其布尔操作数的逻辑与,但仅在必要时评估其第二个操作数。条件和运算符 (&&) MSDN
Here is a set of sections listing the C# operators starting with the highest precedence to the lowest.
下面是一组列出 C# 运算符的部分,从最高优先级到最低优先级。

