比較運算子 (C#參考)Comparison operators (C# reference)

< (小於) > (大於) <= (小於或等於) ,而且 >= (大於或等於) 比較(也稱為關聯式),運算子會比較其運算元。The < (less than), > (greater than), <= (less than or equal), and >= (greater than or equal) comparison, also known as relational, operators compare their operands. 所有整數浮點數數值型別都支援這些運算子。Those operators are supported by all integral and floating-point numeric types.

注意

針對 ==<><=>= 運算子,如果任何運算元不是數字 (Double.NaNSingle.NaN),則作業的結果是 falseFor the ==, <, >, <=, and >= operators, if any of the operands is not a number (Double.NaN or Single.NaN), the result of operation is false. 這代表 NaN 的值皆不會大於、小於或等於任何其他 double (或 float) 的值,包括 NaNThat means that the NaN value is neither greater than, less than, nor equal to any other double (or float) value, including NaN. 如需詳細資訊和範例,請參閱 Double.NaNSingle.NaN 參考文章。For more information and examples, see the Double.NaN or Single.NaN reference article.

Char類型也支援比較運算子。The char type also supports comparison operators. 在運算元的案例中 char ,會比較對應的字元碼。In the case of char operands, the corresponding character codes are compared.

列舉類型也支援比較運算子。Enumeration types also support comparison operators. 相同列舉類型的運算元會比較基礎整數型別的對應值。For operands of the same enum type, the corresponding values of the underlying integral type are compared.

==!= 運算子會檢查其運算元是否相等。The == and != operators check if their operands are equal or not.

小於運算子 <Less than operator <

< 運算子會傳回 true (如果其左邊運算元小於第右邊運算元),否則會傳回 falseThe < operator returns true if its left-hand operand is less than its right-hand operand, false otherwise:

Console.WriteLine(7.0 < 5.1);   // output: False
Console.WriteLine(5.1 < 5.1);   // output: False
Console.WriteLine(0.0 < 5.1);   // output: True

Console.WriteLine(double.NaN < 5.1);   // output: False
Console.WriteLine(double.NaN >= 5.1);  // output: False

大於運算子 >Greater than operator >

> 運算子會傳回 true (如果其左邊運算元大於第右邊運算元),否則會傳回 falseThe > operator returns true if its left-hand operand is greater than its right-hand operand, false otherwise:

Console.WriteLine(7.0 > 5.1);   // output: True
Console.WriteLine(5.1 > 5.1);   // output: False
Console.WriteLine(0.0 > 5.1);   // output: False

Console.WriteLine(double.NaN > 5.1);   // output: False
Console.WriteLine(double.NaN <= 5.1);  // output: False

小於或等於運算子 <=Less than or equal operator <=

<= 運算子會傳回 true (如果其左邊運算元小於或等於右邊運算元),否則會傳回 falseThe <= operator returns true if its left-hand operand is less than or equal to its right-hand operand, false otherwise:

Console.WriteLine(7.0 <= 5.1);   // output: False
Console.WriteLine(5.1 <= 5.1);   // output: True
Console.WriteLine(0.0 <= 5.1);   // output: True

Console.WriteLine(double.NaN > 5.1);   // output: False
Console.WriteLine(double.NaN <= 5.1);  // output: False

大於或等於運算子 >=Greater than or equal operator >=

>= 運算子會傳回 true (如果其左邊運算元大於或等於右邊運算元),否則會傳回 falseThe >= operator returns true if its left-hand operand is greater than or equal to its right-hand operand, false otherwise:

Console.WriteLine(7.0 >= 5.1);   // output: True
Console.WriteLine(5.1 >= 5.1);   // output: True
Console.WriteLine(0.0 >= 5.1);   // output: False

Console.WriteLine(double.NaN < 5.1);   // output: False
Console.WriteLine(double.NaN >= 5.1);  // output: False

運算子是否可多載Operator overloadability

使用者定義型別可以多<><=>= 運算子。A user-defined type can overload the <, >, <=, and >= operators.

如果類型多載 <> 運算子之一,則也必須多載 <>If a type overloads one of the < or > operators, it must overload both < and >. 如果類型多載 <=>= 運算子之一,則也必須多載 <=>=If a type overloads one of the <= or >= operators, it must overload both <= and >=.

C# 語言規格C# language specification

如需詳細資訊,請參閱 C# 語言規格關係及類型測試運算子一節。For more information, see the Relational and type-testing operators section of the C# language specification.

另請參閱See also