How do nullable types handle null values ​​with comparison operators?

Does anyone have specific information on how C # handles comparisons with Nullable<T> types when one side of the comparison is null?

As I understand from experiments with the compiler, it seems that the comparison always returns false, but I can not find the documentation confirming this. Is this a real feature of the language (and therefore something that I can count on), or are these implementation details that may change in future versions?

In other words, does the following method return true y.HasValue , and can you point me to some documentation confirming that this is happening?

  public bool foo(int x, int? y) { return x < y; } 
+18
c #
Feb 29 2018-12-12T00:
source share
5 answers

Does anyone have specific information on how C # handles comparisons with Nullable types when one side of the comparison is null?

Yes - C # Language Specification, Section 7.3.7. In this case, it is a relational operator:

For relation operators < > <= >= <=> < > <= >= there is an elevated form of the operator if the operand types are non-infectable types and if the result type is bool . Is the raised form built by adding a single modifier ? to each type of operand. The raised statement returns false if one or both operands are equal to zero. Otherwise, the captured operator expands the operands and applies the main operator to get the result bool .

Similar detailed sections for other operators.

If you doubt how any aspect of the language works (and whether it is guaranteed or specific to a particular implementation), the C # language specification should be your first port of call.

+41
Feb 29 2018-12-12T00:
source share

If one of the values ​​is null, the comparison will be false (except != )

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.

MSDN source

+5
Feb 29 2018-12-28T00:
source share

MSDN has the following:

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 are evaluated as false, with the exception of: = (not equal).

http://msdn.microsoft.com/en-us/library/2cf62fcy(v=vs.100).aspx

Here are the sample code:

 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"); } 
+2
Feb 29 '12 at 22:16
source share

If a specific CompareTo is not implemented, then my research tells me that the object will use CompareTo (object). In the case of int you can compare with int or object. In this case, it only checks if the object is null or not. Here is a link to int CompareTo (object), it details the reason for the results of comparing int and object.

http://msdn.microsoft.com/en-us/library/system.int32.aspx

I can’t find anything for sure, but I don’t see anything that indicates that the .NET platform has been expanded to include the CompareTo method for System.Nullable<T> for each <T> .

If this were my code, I would protect myself and extend the Int class by including CompareTo.

0
Feb 29 2018-12-29T00:
source share

I know I'm late, but I'll drop my two cents.

If memory is used, the rules for comparison are as follows:

  • If x and y are zero, return 0 (they are equal).
  • If x is null and y is not, return -1 (x is less than y).
  • If x is not null and y is null, return 1 (x is greater than y).

For all non-empty values:

  • When the value of x is evaluated less than y, return -1.
  • When the value of x evaluates y, return 0.
  • When x is greater than y, return 1.

For all purposes, Nullable <T> evaluates to null when it does not matter. Thus, the rules are essentially the same. At least this is how I wrote my comparisons. If I do it wrong, then, holy god, I do it wrong, and I'm sure someone will tell me how to fix it!

0
Feb 29 2018-12-12T00:
source share



All Articles