相等运算符(C# 参考)Equality operators (C# reference)

==(相等)!=(不等) 运算符检查其操作数是否相等。The == (equality) and != (inequality) operators check if their operands are equal or not.

相等运算符 ==Equality operator ==

如果操作数相等,等于运算符 == 返回 true,否则返回 falseThe equality operator == returns true if its operands are equal, false otherwise.

值类型的相等性Value types equality

如果内置值类型的值相等,则其操作数相等:Operands of the built-in value types are equal if their values are equal:

int a = 1 + 2 + 3;
int b = 6;
Console.WriteLine(a == b);  // output: True

char c1 = 'a';
char c2 = 'A';
Console.WriteLine(c1 == c2);  // output: False
Console.WriteLine(c1 == char.ToLower(c2));  // output: True

备注

对于 ==<><=>= 运算符,如果任何操作数不是数字(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.

如果基本整数类型的相应值相等,则相同枚举类型的两个操作数相等。Two operands of the same enum type are equal if the corresponding values of the underlying integral type are equal.

用户定义的 struct 类型默认情况下不支持 == 运算符。User-defined struct types don't support the == operator by default. 要支持 == 运算符,用户定义的结构必须重载它。To support the == operator, a user-defined struct must overload it.

从 C# 7.3 开始,==!= 运算符由 C# 元组支持。Beginning with C# 7.3, the == and != operators are supported by C# tuples. 有关详细信息,请参阅 C# 元组类型一文的相等性和元组部分。For more information, see the Equality and tuples section of the C# tuple types article.

引用类型的相等性Reference types equality

默认情况下,如果两个引用类型操作符引用同一对象,则这两个操作符相等:By default, two reference-type operands are equal if they refer to the same object:

public class ReferenceTypesEquality
{
    public class MyClass
    {
        private int id;

        public MyClass(int id) => this.id = id;
    }

    public static void Main()
    {
        var a = new MyClass(1);
        var b = new MyClass(1);
        var c = a;
        Console.WriteLine(a == b);  // output: False
        Console.WriteLine(a == c);  // output: True
    }
}

如示例所示,默认情况下,用户定义的引用类型支持 == 运算符。As the example shows, user-defined reference types support the == operator by default. 但是,引用类型可重载 == 运算符。However, a reference type can overload the == operator. 如果引用类型重载 == 运算符,使用 Object.ReferenceEquals 方法来检查该类型的两个引用是否引用同一对象。If a reference type overloads the == operator, use the Object.ReferenceEquals method to check if two references of that type refer to the same object.

字符串相等性String equality

如果两个字符串均为 null 或者两个字符串实例具有相等长度且在每个字符位置有相同字符,则这两个字符串操作数相等:Two string operands are equal when both of them are null or both string instances are of the same length and have identical characters in each character position:

string s1 = "hello!";
string s2 = "HeLLo!";
Console.WriteLine(s1 == s2.ToLower());  // output: True

string s3 = "Hello!";
Console.WriteLine(s1 == s3);  // output: False

这就是区分大小写的序号比较。That is a case-sensitive ordinal comparison. 有关字符串比较的详细信息,请参阅如何在 C# 中比较字符串For more information about string comparison, see How to compare strings in C#.

委托相等Delegate equality

当两个委托操作数都是 null 或它们的调用列表长度相同并且在每个位置具有相同的条目时,运行时类型相同的两个委托操作数是相等的:Two delegate operands of the same runtime type are equal when both of them are null or their invocation lists are of the same length and have equal entries in each position:

Action a = () => Console.WriteLine("a");

Action b = a + a;
Action c = a + a;
Console.WriteLine(object.ReferenceEquals(b, c));  // output: False
Console.WriteLine(b == c);  // output: True

有关详细信息,请参阅 C# 语言规范中的委托相等运算符部分。For more information, see the Delegate equality operators section of the C# language specification.

通过计算语义上相同的 Lambda 表达式生成的委托不相等,如以下示例所示:Delegates that are produced from evaluation of semantically identical lambda expressions are not equal, as the following example shows:

Action a = () => Console.WriteLine("a");
Action b = () => Console.WriteLine("a");

Console.WriteLine(a == b);  // output: False
Console.WriteLine(a + b == a + b);  // output: True
Console.WriteLine(b + a == a + b);  // output: False

不等运算符 !=Inequality operator !=

如果操作数不相等,不等于运算符 != 返回 true,否则返回 falseThe inequality operator != returns true if its operands are not equal, false otherwise. 对于内置类型的操作数,表达式 x != y 生成与表达式 !(x == y) 相同的结果。For the operands of the built-in types, the expression x != y produces the same result as the expression !(x == y). 有关类型相等性的更多信息,请参阅相等运算符部分。For more information about type equality, see the Equality operator section.

下面的示例演示 != 运算符的用法:The following example demonstrates the usage of the != operator:

int a = 1 + 1 + 2 + 3;
int b = 6;
Console.WriteLine(a != b);  // output: True

string s1 = "Hello";
string s2 = "Hello";
Console.WriteLine(s1 != s2);  // output: False

object o1 = 1;
object o2 = 1;
Console.WriteLine(o1 != o2);  // output: True

运算符可重载性Operator overloadability

用户定义类型可以重载 ==!= 运算符。A user-defined type can overload the == and != operators. 如果某类型重载这两个运算符之一,它还必须重载另一个运算符。If a type overloads one of the two operators, it must also overload another one.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范中的关系和类型测试运算符部分。For more information, see the Relational and type-testing operators section of the C# language specification.

请参阅See also