Skip to main content

How to Resolve Warning "CS0464: Comparing with null of type 'int' (or other type) always produces 'false'" in C#

The Compiler Warning CS0464 is a code quality and logic warning. The message reads: "Comparing with null of type 'int' (or other type) always produces 'false'".

This warning appears when you perform a comparison (like < or >) between a Nullable Value Type (like int?) and null. While checking for equality (== null) is valid, relational comparisons with null in C# (according to the language specification) always return false. This logic is often counter-intuitive and usually indicates a bug where the developer expected the comparison to handle missing values differently.

This guide explains the rules of "Lifted Operators" involving null and how to write correct comparison logic.

Understanding Comparisons with Null

In C#, standard comparison operators (<, >, <=, >=) are "lifted" to support nullable types (int?, double?). The rule for these operators is simple: If one or both operands are null, the result is false.

  • 5 > null evaluates to false.
  • 5 < null evaluates to false.
  • null >= null evaluates to false.

Consequently, writing code that explicitly asks "Is this number greater than null?" serves no purpose because the answer is hardcoded to false by the language definition.

note

Equality Exception: The equality operators == and != do work as expected. null == null is true. Warning CS0464 only applies to relational comparisons (greater than / less than).

Scenario: The Phantom Logic

This warning usually appears when a developer tries to treat null as if it were zero or negative infinity during a comparison.

Example of warning:

public void CheckScore(int? score)
{
// ⛔️ Warning CS0464: Comparing with null of type 'int?' always produces 'false'.
// The developer might expect this to mean "If score is not set (null), treat it as 0".
// But this condition will NEVER be true.
if (score < null)
{
System.Console.WriteLine("Score is negative or missing.");
}
}

Solution 1: Check HasValue Explicitly

If your logic depends on whether the value exists, check for null (or .HasValue) separately from the numeric comparison.

Solution: explicitly handle the "missing value" case.

public void CheckScore(int? score)
{
// ✅ Correct: Handle null separately.
// Logic: If it is null OR if it is less than 0.
if (score == null || score < 0)
{
System.Console.WriteLine("Score is negative or missing.");
}
}

Solution 2: Use Null-Coalescing

If you intend for null to represent a default value (like 0), use the null-coalescing operator (??) to convert the nullable type into a non-nullable type before comparing.

Solution: convert null to 0 on the fly.

public void CheckScore(int? score)
{
// ✅ Correct: If score is null, use 0. Then compare 0 < 0 (False).
// If score is -5, -5 < 0 (True).
if ((score ?? 0) < 0)
{
System.Console.WriteLine("Score is strictly negative.");
}
}
tip

Default Value Logic: This approach is best when null semantically means "Default" or "Empty". If null means "Unknown", Solution 1 is safer.

Conclusion

CS0464 warns you that you are asking a question with a fixed answer.

  1. Understand the Rule: x > null is always false.
  2. Avoid Literals: Never write < null or > null in your code.
  3. Define Intent:
    • If null means "Zero", use (val ?? 0) < 5.
    • If null is a specific case, use val == null || val < 5.