布林值邏輯運算子 (C# 參考)Boolean logical operators (C# reference)

下列運算子會搭配 bool 運算元執行邏輯作業:The following operators perform logical operations with the bool operands:

針對整數型別的運算元,&|^ 運算子都會執行位元邏輯運算。For the operands of the integral types, the &, |, and ^ operators perform bitwise logical operations. 如需詳細資訊,請參閱位元與移位運算子For more information, see Bitwise and shift operators.

邏輯否定運算子 !Logical negation operator !

! 運算子會計算其運算元的邏輯否定。The ! operator computes logical negation of its operand. 也就是說,它會在運算元評估為 false 時產生 true,並在運算元評估為 true 時產生 falseThat is, it produces true, if the operand evaluates to false, and false, if the operand evaluates to true:

bool passed = false;
Console.WriteLine(!passed);  // output: True
Console.WriteLine(!true);    // output: False

邏輯 AND 運算子 &Logical AND operator &

& 運算子會計算其運算元的邏輯 AND。The & operator computes the logical AND of its operands. xy 皆求出 true,那麼 x & y 的結果會是 trueThe result of x & y is true if both x and y evaluate to true. 否則,結果為 falseOtherwise, the result is false.

即使左邊運算元的值為 false& 運算子仍會求這兩個運算元的值,所以不論右邊運算元的值為何,其結果必定為 falseThe & operator evaluates both operands even if the left-hand operand evaluates to false, so that the result must be 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;
}

bool a = false & SecondOperand();
Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// False

bool b = true & SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True

條件邏輯 AND 運算子 && 也會計算其運算元的邏輯 AND,但如果右邊運算元的值評估為 false,系統便不會評估左邊運算元。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.

針對整數型別的運算元,& 運算子會計算其運算元的位元邏輯 ANDFor the operands of the integral types, the & operator computes the bitwise logical AND of its operands. 一元的 & 運算子是 address-of 運算子The unary & operator is the address-of operator.

邏輯互斥 OR 運算子 ^Logical exclusive OR operator ^

^ 運算子會計算其運算元的邏輯互斥 OR,其也稱為邏輯 XOR。The ^ operator computes the logical exclusive OR, also known as the logical XOR, of its operands. 如果 x 評估為 truey 評估為 false,或是 x 評估為 falsey 評估為 true 時,x ^ y 的結果將會為 trueThe 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. 否則,結果為 falseOtherwise, the result is false. 也就是說,針對 bool 運算元,^ 運算子的計算結果會與不等比較運算子 != 相同。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

針對整數型別的運算元,^ 運算子會計算其運算元的位元邏輯互斥 ORFor the operands of the integral types, the ^ operator computes the bitwise logical exclusive OR of its operands.

邏輯 OR 運算子 |Logical OR operator |

| 運算子會計算其運算元的邏輯 OR。The | operator computes the logical OR of its operands. xy 其中一項的值為 truex | y 的結果會是 trueThe result of x | y is true if either x or y evaluates to true. 否則,結果為 falseOtherwise, the result is false.

即使左邊運算元的值為 true| 運算子仍會求這兩個運算元的值,所以不論右邊運算元的值為何,其結果必定為 trueThe | operator evaluates both operands even if the left-hand operand evaluates to true, so that the result must be 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;
}

bool a = true | SecondOperand();
Console.WriteLine(a);
// Output:
// Second operand is evaluated.
// True

bool b = false | SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True

條件式邏輯 OR 運算子 || 也會計算其運算元的邏輯 OR,但如果左邊運算元的值評估為 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.

針對整數型別的運算元,| 運算子會計算其運算元的位元邏輯 ORFor the operands of the integral types, the | operator computes the bitwise logical OR of its operands.

條件式邏輯 AND 運算子 &&Conditional logical AND operator &&

條件邏輯 AND 運算子 &&,也稱為「捷徑運算」邏輯 AND 運算子,會計算其運算元的邏輯 AND。The conditional logical AND operator &&, also known as the "short-circuiting" logical AND operator, computes the logical AND of its operands. xy 皆求出 true,那麼 x && y 的結果會是 trueThe result of x && y is true if both x and y evaluate to true. 否則,結果為 falseOtherwise, the result is false. 如果 x 評估為 false,則不會評估 yIf x evaluates to false, y is not evaluated.

在下列範例中,&& 運算子的右邊運算元是方法呼叫;如果左邊運算元的值評估為 false,系統便不會執行該呼叫:In the following example, the right-hand operand of the && operator is a method call, which isn't performed if the left-hand operand evaluates to false:

bool SecondOperand()
{
    Console.WriteLine("Second operand is evaluated.");
    return true;
}

bool a = false && SecondOperand();
Console.WriteLine(a);
// Output:
// False

bool b = true && SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True

邏輯 AND 運算子 & 也會計算其運算元的邏輯 AND,但一律會求兩個運算元的值。The logical AND operator & also computes the logical AND of its operands, but always evaluates both operands.

條件邏輯 OR 運算子 ||Conditional logical OR operator ||

條件邏輯 OR 運算子 ||,也稱為「捷徑運算」邏輯 OR 運算子,會計算其運算元的邏輯 OR。The conditional logical OR operator ||, also known as the "short-circuiting" logical OR operator, computes the logical OR of its operands. xy 其中一項的值為 truex || y 的結果會是 trueThe result of x || y is true if either x or y evaluates to true. 否則,結果為 falseOtherwise, the result is false. 如果 x 評估為 true,則不會評估 yIf x evaluates to true, y is not evaluated.

在下列範例中,|| 運算子的右邊運算元是方法呼叫;如果左邊運算元的值評估為 true,系統便不會執行該呼叫:In the following example, the right-hand operand of the || operator is a method call, which isn't performed if the left-hand operand evaluates to true:

bool SecondOperand()
{
    Console.WriteLine("Second operand is evaluated.");
    return true;
}

bool a = true || SecondOperand();
Console.WriteLine(a);
// Output:
// True

bool b = false || SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True

邏輯 OR 運算子 | 也會計算其運算元的邏輯 OR,但一律會求兩個運算元的值。The logical OR operator | also computes the logical OR of its operands, but always evaluates both operands.

可為 Null 的布林值邏輯運算子Nullable Boolean logical operators

針對 bool? 運算元,&| 運算子支援三值邏輯。For the bool? operands, the & and | operators support the three-valued logic. 這些運算子的語意是由下列表格定義的:The semantics of these operators is defined by the following table:

xx yy x&yx&y x|yx|y
truetrue truetrue truetrue truetrue
truetrue Falsefalse falsefalse truetrue
truetrue nullnull nullnull truetrue
Falsefalse truetrue Falsefalse truetrue
Falsefalse Falsefalse Falsefalse Falsefalse
Falsefalse nullnull Falsefalse nullnull
nullnull truetrue nullnull truetrue
nullnull Falsefalse Falsefalse nullnull
nullnull nullnull nullnull nullnull

那些運算子的行為和具有可為 Null 實值類型之一般運算子的行為並不相同。The behavior of those operators differs from the typical operator behavior with nullable value types. 一般而言,已針對某個實值類型之運算元定義的運算子,也可以搭配相對應可為 Null 實值類型的運算元使用。Typically, an operator which is defined for operands of a value type can be also used with operands of the corresponding nullable value type. 這種運算子會在其任何一個運算元為 null 的情況下產生 nullSuch an operator produces null if any of its operands is null. 不過,就算其中一個運算元是 null&| 運算子仍可以產生非 Null。However, the & and | operators can produce non-null even if one of the operands is null. 如需具備可為 Null 實值類型之運算子行為的詳細資訊,請參閱使用可為 Null 的類型一文中的運算子一節。For more information about the operator behavior with nullable value types, see the Operators section of the Using nullable types article.

您也可以使用 !^ 運算子搭配 bool? 運算元,如下列範例所示︰You can also use the ! and ^ operators with the bool? operands, as the following example shows:

bool? test = null;
Display(!test);         // output: null
Display(test ^ false);  // output: null
Display(test ^ null);   // output: null
Display(true ^ null);   // output: null 

void Display(bool? b) => Console.WriteLine(b is null ? "null" : b.Value.ToString());

條件邏輯運算子 &&|| 不支援 bool? 運算元。The conditional logical operators && and || don't support the bool? operands.

複合指派Compound assignment

若是二元運算子 op,表單的複合指派運算式For a binary operator op, a compound assignment expression of the form

x op= y

相當於is equivalent to

x = x op y

但只會評估 x 一次。except that x is only evaluated once.

&|^ 運算子支援複合指派,如下列範例所示:The &, |, and ^ operators support compound assignment, as the following example shows:

bool test = true;
test &= false;
Console.WriteLine(test);  // output: False

test |= true;
Console.WriteLine(test);  // output: True

test ^= false;
Console.WriteLine(test);  // output: True

條件邏輯運算子 &&|| 不支援複合指派。The conditional logical operators && and || don't support compound assignment.

運算子優先順序Operator precedence

下列清單會依優先順序將邏輯運算子排序 (從最高到最低):The following list orders logical operators starting from the highest precedence to the lowest:

  • 邏輯否定運算子 !Logical negation operator !
  • 邏輯 AND 運算子 &Logical AND operator &
  • 邏輯互斥 OR 運算子 ^Logical exclusive OR operator ^
  • 邏輯 OR 運算子 |Logical OR operator |
  • 條件邏輯 AND 運算子 &&Conditional logical AND operator &&
  • 條件邏輯 OR 運算子 ||Conditional logical OR operator ||

使用括號 () 來變更由運算子優先順序所強制執行的評估順序:Use parentheses, (), to change the order of evaluation imposed by operator precedence:

Console.WriteLine(true | true & false);   // output: True
Console.WriteLine((true | true) & false); // output: False

bool Operand(string name, bool value)
{
    Console.WriteLine($"Operand {name} is evaluated.");
    return value;
}

var byDefaultPrecedence = Operand("A", true) || Operand("B", true) && Operand("C", false);
Console.WriteLine(byDefaultPrecedence);
// Output:
// Operand A is evaluated.
// True

var changedOrder = (Operand("A", true) || Operand("B", true)) && Operand("C", false);
Console.WriteLine(changedOrder);
// Output:
// Operand A is evaluated.
// Operand C is evaluated.
// False

如需按優先順序層級排序的 C# 運算子完整清單,請參閱 C# 運算子For the complete list of C# operators ordered by precedence level, see C# operators.

運算子是否可多載Operator overloadability

使用者定義類型可以多載 !&|^ 運算子。A user-defined type can overload the !, &, |, and ^ operators. 當二元運算子多載時,對應的複合指派運算子也會隱含地多載。When a binary operator is overloaded, the corresponding compound assignment operator is also implicitly overloaded. 使用者定義型別無法明確地多載複合指派運算子。A user-defined type cannot explicitly overload a compound assignment operator.

使用者定義類型無法多載條件邏輯運算子 &&||A user-defined type cannot overload the conditional logical operators && and ||. 不過,若使用者定義類型以某種方式多載 True 和 False 運算子以及 &| 運算子,就可以針對該類型的運算元個別評估 &&|| 作業。However, if a user-defined type overloads the true and false operators and the & or | operator in a certain way, the && or || operation, respectively, can be evaluated for the operands of that type. 如需詳細資訊,請參閱 C# 語言規格使用者定義條件式邏輯運算子一節。For more information, see the User-defined conditional logical operators section of the C# language specification.

C# 語言規格C# language specification

如需詳細資訊,請參閱 C# 語言規格的下列幾節:For more information, see the following sections of the C# language specification:

另請參閱See also