== 運算子 (C# 參考)== Operator (C# Reference)

等號比較運算子 == 會在其運算元相等時傳回 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 relational operators ==, >, <, >=, and <=, if any of the operands is not a number (Double.NaN or Single.NaN) the result of operation is false. 這代表 NaN 的值皆不會大於、小於或等於任何其他 double (或 float) 的值。That means that the NaN value is neither greater than, less than, nor equal to any other double (or float) value. 如需詳細資訊和範例,請參閱 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 類型。By default, the == operator is not defined for a user-defined struct type. 使用者定義型別可以多載 == 運算子。A user-defined type can overload the == operator.

從 C# 7.3 說起,==!= 運算子是由 C# tuples 所支援。Beginning with C# 7.3, the == and != operators are supported by C# tuples. 如需詳細資訊,請參閱 C# Tuple 類型一文中的相等與 Tuple一節。For more information, see the Equality and tuples section of the C# tuple types article.

字串相等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 case-sensitive ordinal comparison. 如需如何比較字串的詳細資訊,請參閱如何在 C# 中比較字串For more information about how to compare strings, see How to compare strings in C#.

參考型別相等Reference types equality

當兩個不是 string 參考型別的運算元參考相同的物件時,兩者就會相等:Two other than string reference type operands are equal when 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
    }
}

此範例顯示出使用者定義參考型別會支援 == 運算子。The example shows that the == operator is supported by user-defined reference types. 不過,使用者定義參考型別可以多載 == 運算子。However, a user-defined 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.

運算子是否可多載Operator overloadability

使用者定義型別可以多載 == 運算子。User-defined types can overload the == operator. 若類型多載等號比較運算子 ==,其必須也多載不等比較運算子 !=If a type overloads the equality operator ==, it must also overload the inequality operator !=.

C# 語言規格C# language specification

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

另請參閱See also