Chapter 1
Chapter 1
Chapter 1
double a = 1.5;
Console.WriteLine(a); // output: 1.5
Console.WriteLine(++a); // output: 2.5
Console.WriteLine(a); // output: 2.5
The & operator computes the logical AND of its operands. The result of x & y is true if both x
and y evaluate to true. Otherwise, the result is false.
The & operator evaluates both operands even if the left-hand operand evaluates to false, so that
the operation result is false regardless of the value of the right-hand operand.
In the following example, the right-hand operand of the & operator is a method call, which is
performed regardless of the value of the left-hand operand:
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}
The conditional logical AND operator && also computes the logical AND of its operands, but
doesn't evaluate the right-hand operand if the left-hand operand evaluates to false.
The ^ operator computes the logical exclusive OR, also known as the logical XOR, of its
operands. The result of x ^ y is true if x evaluates to true and y evaluates to false, or x evaluates
to false and y evaluates to true. Otherwise, the result is false. That is, for the bool operands, the ^
operator computes the same result as the inequality operator !=.
Console.WriteLine(true ^ true); // output: False
Console.WriteLine(true ^ false); // output: True
Console.WriteLine(false ^ true); // output: True
Console.WriteLine(false ^ false); // output: False
The | operator computes the logical OR of its operands. The result of x | y is true if either x or y
evaluates to true. Otherwise, the result is false.
The | operator evaluates both operands even if the left-hand operand evaluates to true, so that the
operation result is true regardless of the value of the right-hand operand.
In the following example, the right-hand operand of the | operator is a method call, which is
performed regardless of the value of the left-hand operand:
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}
The conditional logical OR operator || also computes the logical OR of its operands, but doesn't
evaluate the right-hand operand if the left-hand operand evaluates to true.
bool SecondOperand()
{
Console.WriteLine("Second operand is evaluated.");
return true;
}
is evaluated as
a = (b = c)