!= Operator (C# Reference)

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, see the == Operator article.

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

User-defined types can overload the != operator. If a type overloads the inequality operator !=, it must also overload the equality operator ==.

C# language specification

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

See also