C#If语句中条件的执行顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16711051/
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
Execution order of conditions in C# If statement
提问by LCJ
There are two if statements below that have multiple conditions using logical operators. Logically both are same but the order of check differs. The first one works and the second one fails.
下面有两个 if 语句使用逻辑运算符具有多个条件。逻辑上两者相同,但检查顺序不同。第一个有效,第二个失败。
I referred MSDNfor checking whether the order of execution of the conditions defined; but I could not find.
我参考了MSDN来检查是否定义了条件的执行顺序;但我找不到。
Consider a multiple check condition that has &&as the logical operator. Is it guaranteed that it will always check the first condition and if that is not satisfied the second condition will notbe checked?
考虑具有&&作为逻辑运算符的多重检查条件。是否保证它总是检查第一个条件,如果不满足第二个条件将不会被检查?
I used to use approach 1 and it works well. Looking for an MSDN reference substantiaing the use.
我曾经使用方法 1 并且效果很好。寻找证实使用的 MSDN 参考。
UPDATE
更新
Refer "short-circuit" evaluation
参考“短路”评估
CODE
代码
List<string> employees = null;
if (employees != null && employees.Count > 0)
{
string theEmployee = employees[0];
}
if (employees.Count > 0 && employees != null)
{
string theEmployee = employees[0];
}
采纳答案by Patashu
The && and || operators short-circuit. That is:
&& 和 || 运营商短路。那是:
1) If && evaluates its first operand as false, it does not evaluate its second operand.
1) 如果 && 将其第一个操作数计算为假,则不会计算其第二个操作数。
2) If || evaluates its first operand as true, it does not evaluate its second operand.
2) 如果|| 评估其第一个操作数为真,它不评估其第二个操作数。
This lets you do null check && do something with object, as if it is not null the second operand is not evaluated.
这使您可以进行空检查 && 对对象执行某些操作,就好像它不为空一样,不会评估第二个操作数。
回答by Darren
You should use:
你应该使用:
if (employees != null && employees.Count > 0)
{
string theEmployee = employees[0];
}
&&will shortcircuit and employees.Countwill not be execucted if employeesis null.
&&employees.Count如果employees是,将短路并且不会被执行null。
In your second example, the application will throw an exception if employeesis nullwhen you attempt to Countthe elements in the collection.
在第二个例子中,如果应用程序会抛出异常employees是null当您尝试Count集合中的元素。
http://msdn.microsoft.com/en-us/library/2a723cdk(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/2a723cdk(v=vs.71).aspx
回答by Frank Rem
The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.
条件与运算符 (&&) 执行其布尔操作数的逻辑与,但仅在必要时评估其第二个操作数。
回答by John Willemse
The conditions are checked left to right. The &&operator will only evaluate the right condition if the left condition is true.
从左到右检查条件。&&如果左侧条件为真,运算符将仅评估右侧条件。
Section 5.3.3.24 of the C# Language Specification states:
C# 语言规范的第 5.3.3.24 节指出:
5.3.3.24 && expressions
For an expression exprof the form
expr-first && expr-second:· The definite assignment state of v before
expr-firstis the same as the definite assignment state of v before expr.· The definite assignment state of v before
expr-secondis definitely assigned if the state of v afterexpr-firstis either definitely assigned or “definitely assigned after true expression”. Otherwise, it is not definitely assigned.· The definite assignment state of v after expr is determined by:
o If the state of v after
expr-firstis definitely assigned, then the state of v after expr is definitely assigned.o Otherwise, if the state of v after
expr-secondis definitely assigned, and the state of v afterexpr-firstis “definitely assigned after false expression”, then the state of v after expr is definitely assigned.o Otherwise, if the state of v after
expr-secondis definitely assigned or “definitely assigned after true expression”, then the state of v after expr is “definitely assigned after true expression”.o Otherwise, if the state of v after
expr-firstis “definitely assigned after false expression”, and the state of v afterexpr-secondis “definitely assigned after false expression”, then the state of v after expr is “definitely assigned after false expression”.o Otherwise, the state of v after expr is not definitely assigned.
5.3.3.24 && 表达式
为表达式expr的形式
expr-first && expr-second:· v之前
expr-first的明确赋值状态与expr之前v的明确赋值状态相同。·
expr-second如果v after 的状态expr-first是明确赋值或“true expression 后明确赋值” ,则v before 的明确赋值状态是明确赋值的。否则,它不会被明确分配。· expr 之后 v 的明确赋值状态由下式决定:
o 如果 v 之后的状态
expr-first是明确赋值的,那么 expr 之后 v 的状态也是明确赋值的。o 否则,如果 v after 的状态
expr-second是确定赋值的,并且 v after 的状态expr-first是“false expression 后确定赋值”,那么 expr 之后 v 的状态是确定赋值的。o 否则,如果 v 之后的状态
expr-second是明确赋值的或者“真表达式之后确定赋值”,那么 expr 之后 v 的状态就是“真表达式之后确定赋值”。o 否则,如果 v after 的状态
expr-first是“假表达式后确定赋值”,而 v after 的状态expr-second是“假表达式后确定赋值”,则 expr 后 v 的状态为“假表达式后确定赋值”。o 否则,expr 之后 v 的状态不是明确赋值的。
So this makes it clear that expr-firstis always evaluated and if true then, and only then, expr-secondis also evaluated.
所以这清楚地表明expr-first总是被评估,如果为真,那么,expr-second也被评估。
回答by G.Y
left to right while expression is still questionable.
从左到右,而表达仍然值得怀疑。
回答by yclkvnc
Is it guaranteed that it will always check the first condition and if that is not satisfied the second condition will not be checked?
是否保证它总是检查第一个条件,如果不满足第二个条件将不会被检查?
Short answer is yes.
简短的回答是肯定的。
回答by JeffRSon
See, for example, this MSDN page for &&which describes the short-circuit evaluation.
例如,请参阅此 MSDN 页面的&&,其中描述了短路评估。
You can check or prove execution sequence like this:
您可以像这样检查或证明执行顺序:
int i;
bool b;
b=((i=3)==0 && (i=4)!=0);
Console.WriteLine(i);
b=((i=3)!=0 || (i=4)!=0);
Console.WriteLine(i);
You get 3 in both cases - which shows, that in both cases the short-circuit behaviour takes place. On the other hand, you could use the '&' or '|', respectively, logical operator, to prevent that. Such you will get a result of 4, because both conditions have been evaluated.
在这两种情况下你都会得到 3 - 这表明在这两种情况下都会发生短路行为。另一方面,您可以分别使用 '&' 或 '|' 逻辑运算符来防止这种情况发生。这样您将得到 4 的结果,因为这两个条件都已被评估。
回答by Chirag
The && and || operators are often used to check for object conditions.
&& 和 || 运算符通常用于检查对象条件。
1) The "&&" condition evaluates its first operand as false, it does not evaluate its second operand. If it returns true, the second condition evaluates. If second condition is true, then only it will return true. So && can be used to make sure that all conditions are satisfied as valid.
1) “&&”条件将它的第一个操作数评估为假,它不评估它的第二个操作数。如果返回 true,则计算第二个条件。如果第二个条件为真,则只有它会返回真。因此 && 可用于确保所有条件都满足有效。
2) The "||" condition evaluates its first operand as true, it does not evaluate its second operand. If first condition evaluates as false, then only it will evaluate to second condition. If it is satisfied, then it will return true. Otherwise, false.
2)“||” 条件评估其第一个操作数为真,它不评估其第二个操作数。如果第一个条件评估为假,那么只有它会评估为第二个条件。如果满足,则返回true。否则为假。

