How does comparison operator works with null int?

I am starting to learn nullable types and ran into following behavior.

While trying nullable int, i see comparison operator gives me unexpected result. For example, In my code below, The output i get is “both and 1 are equal”. Note, it does not print “null” as well.

int? a = null;
int? b = 1;

if (a < b)
    Console.WriteLine("{0} is bigger than {1}", b, a);
else if (a > b)
    Console.WriteLine("{0} is bigger than {1}", a, b);
else
    Console.WriteLine("both {0} and {1} are equal", a, b);

I was hoping any non-negative integer would be greater than null, Am i missing something here?

4 Answers
4

Leave a Comment