Equality operators (C# reference)

The == (equality) and != (inequality) operators check if their operands are equal or not.

Equality operator ==

The 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

Note

For the ==, <, >, <=, and >= operators, if any of the operands is not a number (Double.NaN or Single.NaN), the result of operation is false. That means that the NaN value is neither greater than, less than, nor equal to any other double (or float) value, including 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.

Beginning with C# 7.3, the == and != operators are supported by C# tuples. 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. 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

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. For more information about string comparison, see How to compare strings in C#.

Delegate equality

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

For more information, see the Delegate equality operators section of the C# language specification.

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 !=

The inequality operator != returns true if its operands are not equal, false otherwise. 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# language specification

For more information, see the Relational and type-testing operators section of the C# language specification.

See also