比较运算符(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.

枚举类型也支持比较运算符。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