How does comparison operator works with null int?
Asked Answered
M

5

216

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?

Motorist answered 3/4, 2013 at 2:14 Comment(2)
Here is a small .NET Fiddle to see several cases.Emogene
@UweKeim, in the fiddle, the second overload should be void compare(int? a, int b), i.e., the second parameter should be non-nullable.Delores
W
308

According to MSDN - it's down the page in the "Operators" section:

When you perform comparisons with nullable types, if the value of one of the nullable types is null and the other is not, all comparisons evaluate to false except for !=

So both a > b and a < b evaluate to false since a is null...

Whisky answered 3/4, 2013 at 2:16 Comment(2)
In my projects I am currently using VB.NET and it seems that nothing <> 1 = null in VB whereas null != 1 = true in C# - I have been using LinqPad to test the statementsTolerance
I just wondered, and it's worth noting about Lifted Operators in C# https://mcmap.net/q/128412/-what-are-lifted-operators - I wondered if this might be the reason VB.NET returns different resultsTolerance
R
61

As MSDN says

When you perform comparisons with nullable types, if the value of one of the nullable types is null and the other is not, all comparisons evaluate to false except for != (not equal). It is important not to assume that because a particular comparison returns false, the opposite case returns true. In the following example, 10 is not greater than, less than, nor equal to null. Only num1 != num2 evaluates to true.

int? num1 = 10;
int? num2 = null;
if (num1 >= num2)
{
    Console.WriteLine("num1 is greater than or equal to num2");
}
else
{
    // This clause is selected, but num1 is not less than num2.
    Console.WriteLine("num1 >= num2 returned false (but num1 < num2 also is false)");
}

if (num1 < num2)
{
    Console.WriteLine("num1 is less than num2");
}
else
{
    // The else clause is selected again, but num1 is not greater than 
    // or equal to num2.
    Console.WriteLine("num1 < num2 returned false (but num1 >= num2 also is false)");
}

if (num1 != num2)
{
    // This comparison is true, num1 and num2 are not equal.
    Console.WriteLine("Finally, num1 != num2 returns true!");
}

// Change the value of num1, so that both num1 and num2 are null.
num1 = null;
if (num1 == num2)
{
    // The equality comparison returns true when both operands are null.
    Console.WriteLine("num1 == num2 returns true when the value of each is null");
}

/* Output:
 * num1 >= num2 returned false (but num1 < num2 also is false)
 * num1 < num2 returned false (but num1 >= num2 also is false)
 * Finally, num1 != num2 returns true!
 * num1 == num2 returns true when the value of each is null
 */
Roger answered 3/4, 2013 at 2:20 Comment(0)
I
45

To summarise: any inequality comparison with null (>=, <, <=, >) returns false even if both operands are null. i.e.

null >  anyValue //false
null <= null     //false

Any equality or non-equality comparison with null (==, !=) works 'as expected'. i.e.

null == null     //true
null != null     //false
null == nonNull  //false
null != nonNull  //true
Infusible answered 9/12, 2016 at 1:9 Comment(2)
Is it same for both int? nonNull and int notNull ?Demonolatry
@КоеКто, The same behavior is for Nullable<NumberTypes> = null. Verified.Mandamandaean
C
6

Comparing C# with SQL

C#: a=null and b=null => a==b => true

SQL: a=null and b=null => a==b => false

Chiropractor answered 6/9, 2019 at 6:8 Comment(0)
A
0

If you do in your last else "else if (a == b)" you wont get any output at all. a is not greater than 1, is not less than 1 or equal to 1, is null

Alpine answered 17/1, 2023 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.