Execution order of conditions in C# If statement

后端 未结 8 1992
孤城傲影
孤城傲影 2020-12-08 19:32

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

8条回答
  •  鱼传尺愫
    2020-12-08 19:58

    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.

提交回复
热议问题