Execution order of conditions in C# If statement

后端 未结 8 1976
孤城傲影
孤城傲影 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条回答
  •  萌比男神i
    2020-12-08 20:16

    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.

提交回复
热议问题