Skip to content
Tutorials Center
My learning

Topics

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.

C#
Console.WriteLine(3 == 3.0);          // True
Console.WriteLine("abc" == "ab" + "c"); // True: string overloads ==
Console.WriteLine(5 >= 6);            // False

Try it

C# Values, references and strings

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.

C# NaN, nullable values and ordering

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

C#
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 == o2 with both variables typed object compares references even if the objects are equal strings. Use Equals or typed variables when you want value comparison.
  • Floating point. NaN is unequal to everything including itself, so x != x would be a (confusing, and warned-about) NaN test. Use double.IsNaN. Never compare computed doubles with ==; use a tolerance or decimal.
  • Nullable value types (lifted operators). == and != treat two nulls as equal; <, >, <=, >= return false when either side is null. So !(a < b) is not the same as a >= b.
  • Strings. == is an ordinal, case-sensitive content comparison. For culture-aware or case-insensitive work, call string.Compare or Equals with a StringComparison. Prefer StringComparison.OrdinalIgnoreCase for identifiers.
  • Records generate == with value equality over all members; structs get no == at all unless you declare it (a == b is a compile error), although a.Equals(b) works.
  • Overloading. If you overload ==, also override Equals(object) and GetHashCode() so dictionaries and sets agree with the operator.
  • Null checks. x == null calls an overloaded ==, while x is null never does. Prefer is null / is not null when 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.