How does a comparison operator with a null int work?

I am starting to learn types with a null value and encounter the following behavior.

When trying a nullable int, I see the comparison operator gives me an unexpected result. For example, in my code below, the output I get is "and 1 are equal . " Please note that it also does not print "zero".

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 that any non-negative integer would be greater than zero, did I miss something here?

+125
c # nullable
Apr 3 '13 at 2:14
source share
4 answers

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

When you perform comparisons with nullable types, if the value of one of the types with a null value is null and the other is not, all comparisons are evaluated using false , except !=

So, both a > b and a < b are evaluated to false , since a is null ...

+180
Apr 3 '13 at 2:16
source share

According to MSDN

When you perform comparisons with NULL types, if the value of one of the null types is null and the other is not, all comparisons evaluate to false, except if = (not equal). It is important not to assume that since a particular comparison returns false, the opposite case returns true. In the following example, 10 is not greater than, less than, or equal to zero. Only num1! = Num2 is 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 */ 
+36
Apr 03 '13 at 2:20
source share

To summarize: any comparison of inequality with zero ( >= , < , <= , > ) returns false , even if both operands are equal to zero. i.e.

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

Any comparison of equality or nonequilibrium with zero ( == != ) Works as expected. i.e.

 null == null //true null != null //false null == nonNull //false null != nonNull //true 
+23
Dec 09 '16 at 1:09
source share

Comparing C # with SQL

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

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

0
Sep 06 '19 at 6:08
source share



All Articles