Execution order of conditions in C# If statement

40,467

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

left to right while expression is still questionable.

Share:
40,467

Related videos on Youtube

LCJ
Author by

LCJ

.Net / C#/ SQL Server Developer Some of my posts listed below -- http://stackoverflow.com/questions/3618380/log4net-does-not-write-the-log-file/14682889#14682889 http://stackoverflow.com/questions/11549943/datetime-field-overflow-with-ibm-data-server-client-v9-7fp5/14215249#14215249 http://stackoverflow.com/questions/12420314/one-wcf-service-two-clients-one-client-does-not-work/12425653#12425653 http://stackoverflow.com/questions/18014392/select-sql-server-database-size/25452709#25452709 http://stackoverflow.com/questions/22589245/difference-between-mvc-5-project-and-web-api-project/25036611#25036611 http://stackoverflow.com/questions/4511346/wsdl-whats-the-difference-between-binding-and-porttype/15408410#15408410 http://stackoverflow.com/questions/7530725/unrecognized-attribute-targetframework-note-that-attribute-names-are-case-sen/18351068#18351068 http://stackoverflow.com/questions/9470013/do-not-use-abstract-base-class-in-design-but-in-modeling-analysis http://stackoverflow.com/questions/11578374/entity-framework-4-0-how-to-see-sql-statements-for-savechanges-method http://stackoverflow.com/questions/14486733/how-to-check-whether-postback-caused-by-a-dynamic-link-button

Updated on July 09, 2022

Comments

  • LCJ
    LCJ almost 2 years

    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];
      }
    
    • David
      David almost 11 years
      search "short circuit"!
  • Bernhard Barker
    Bernhard Barker almost 11 years
    @Lijo Googling "msdn short-circuit", first two results - this and this. Are those sufficient?
  • jrh
    jrh about 5 years