C# Reference ยท Operators and Expressions
C# comparison operators
Reference for C# comparison operators: ==, !=, <, >, <=, >=, value versus reference equality, NaN, string comparison, nullable and lifted operators.
The equality operators == and != test whether two values are the same, and the relational operators <, >, <= and >= order them. All of them return bool.
What == means depends on the type. For numbers, char, bool, enums and structs that define it, it compares values. For most classes it compares references (are these the same object?), unless the class overloads ==. string, records and many framework types overload it to compare content.
Console.WriteLine(3 == 3.0); // True Console.WriteLine("abc" == "ab" + "c"); // True: string overloads == Console.WriteLine(5 >= 6); // False
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
bool equal = left == right; bool notEqual = left != right; bool less = left < right; bool greater = left > right; bool lessEq = left <= right; bool greatEq = left >= right;
| Operator | Description |
|---|---|
== |
true when both operands are equal under the type's rules. |
!= |
The negation of == (but see NaN and nullable notes below). |
<, > |
Less than and greater than. |
<=, >= |
Less than or equal and greater than or equal. |
Returns: bool.
Errors: there is no built-in < for string, object or bool; use string.Compare, CompareTo or IComparable<T>. A custom type that overloads == must also overload != (and < must pair with >), or the code does not compile.
Usage notes
- Static type decides.
o1 == o2with both variables typedobjectcompares references even if the objects are equal strings. UseEqualsor typed variables when you want value comparison. - Floating point.
NaNis unequal to everything including itself, sox != xwould be a (confusing, and warned-about) NaN test. Usedouble.IsNaN. Never compare computed doubles with==; use a tolerance ordecimal. - Nullable value types (lifted operators).
==and!=treat two nulls as equal;<,>,<=,>=returnfalsewhen either side isnull. So!(a < b)is not the same asa >= b. - Strings.
==is an ordinal, case-sensitive content comparison. For culture-aware or case-insensitive work, callstring.CompareorEqualswith aStringComparison. PreferStringComparison.OrdinalIgnoreCasefor identifiers. - Records generate
==with value equality over all members; structs get no==at all unless you declare it (a == bis a compile error), althougha.Equals(b)works. - Overloading. If you overload
==, also overrideEquals(object)andGetHashCode()so dictionaries and sets agree with the operator. - Null checks.
x == nullcalls an overloaded==, whilex is nullnever does. Preferis null/is not nullwhen the type might overload==. - Ranges of values. Combine tests with logical operators or use relational patterns:
age is >= 18 and < 65. - Performance: primitive comparisons are single instructions; string equality first checks length, then content.