bool
variable
someCondition
is initialized to
false
. Next, examine each
if
statement. As the comments indicate, in the first
if
statement,
i
is incremented despite the
fact that
someCondition
is false. When the
&
is used, as it is in the first
if
statement, the
expression on the right side of the
&
is evaluated no matter what value the expression on
the left has. However, in the second
if
statement, the short-circuit operator is used. In this
case, the variable
i
is not incremented because the left operand,
someCondition
, is false,
which causes the expression on the right to be skipped. The lesson here is that if your code
expects the right-hand operand of an AND or OR operation to be evaluated, then you must
use C#’s non-short-circuit forms for these operations.
One other point: The short-circuit AND is also known as the
conditional AND,
and the
short-circuit OR is also called the
conditional OR.
Do'stlaringiz bilan baham: |