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
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-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.
So this makes it clear that expr-first is always evaluated and if true then, and only then, expr-second is also evaluated.