Pergunta

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.

I referred MSDN for checking whether the order of execution of the conditions defined; but I could not find.

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 not be checked?

I used to use approach 1 and it works well. Looking for an MSDN reference substantiaing the use.

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];
  }
Foi útil?

Solução

The && and || operators short-circuit. That is:

1) If && evaluates its first operand as false, it does not evaluate its second operand.

2) If || evaluates its first operand as true, it does not evaluate its second operand.

This lets you do null check && do something with object, as if it is not null the second operand is not evaluated.

Outras dicas

You should use:

  if (employees != null && employees.Count > 0)
  {
        string theEmployee = employees[0];
  }

&& will shortcircuit and employees.Count will not be execucted if employees is null.

In your second example, the application will throw an exception if employees is null when you attempt to Count the elements in the collection.

http://msdn.microsoft.com/en-us/library/2a723cdk(v=vs.71).aspx

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:

5.3.3.24 && expressions

For an expression expr of the form expr-first && expr-second:

· The definite assignment state of v before expr-first is the same as the definite assignment state of v before expr.

· The definite assignment state of v before expr-second is definitely assigned if the state of v after expr-first is 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-first is definitely assigned, then the state of v after expr is definitely assigned.

o Otherwise, if the state of v after expr-second is definitely assigned, and the state of v after expr-first is “definitely assigned after false expression”, then the state of v after expr is definitely assigned.

o Otherwise, if the state of v after expr-second is 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-first is “definitely assigned after false expression”, and the state of v after expr-second is “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.

So this makes it clear that expr-first is always evaluated and if true then, and only then, expr-second is also evaluated.

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.

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.

left to right while expression is still questionable.

The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.

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.

See, for example, this MSDN page for && which describes the short-circuit evaluation.

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top