等號比較運算子 (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.NaN 或) 的數位,作業 Single.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.

使用者定義結構型別預設不支援 == 運算子。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# tuples 所支援。Beginning with C# 7.3, the == and != operators are supported by C# tuples. 如需詳細資訊,請參閱「元組類型」一文的「元組相等」一節。For more information, see the Tuple equality section of the 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

當兩個 string 運算元皆為 null,或兩個 string 執行個體的長度相同且在各字元位置擁有完全相同的字元,兩者就會相等: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 the other one.

C# 語言規格C# language specification

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

另請參閱See also