How does comparison operator works with null int?

前端 未结 4 507
半阙折子戏
半阙折子戏 2020-11-27 04:48

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,

相关标签:
4条回答
  • 2020-11-27 04:56

    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...

    0 讨论(0)
  • 2020-11-27 05:04

    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
     */
    
    0 讨论(0)
  • 2020-11-27 05:06

    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
    
    0 讨论(0)
  • 2020-11-27 05:17

    Comparing C# with SQL

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

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

    0 讨论(0)
提交回复
热议问题