This question already has an answer here:
Why is it that in .NET
null >= null
resolves as false, but
null == null
resolves as true?
In other words, why isn't null >= null
equivalent to null > null || null == null
?
Does anyone have the official answer?
A number of answers appeal to the spec. In what turns out to be an unusual turn of events, the C# 4 spec does not justify the behaviour specifically mentioned of comparison of two null literals. In fact, a strict reading of the spec says that "null == null" should give an ambiguity error! (This is due to an editing error made during the cleanup of the C# 2 specification in preparation for C# 3; it is not the intention of the spec authors to make this illegal.)
Read the spec carefully if you don't believe me. It says that there are equality operators defined on int, uint, long, ulong, bool, decimal, double, float, string, enums, delegates and objects, plus the lifted-to-nullable versions of all the value type operators.
Now, immediately we have a problem; this set is infinitely large. In practice we do not form the infinite set of all operators on all possible delegate and enum types. The spec needs to be fixed up here to note that the only operators on enum and delegate types which are added to the candidate sets are those of enum or delegate types that are the types of either argument.
Let's therefore leave enum and delegate types out of it, since neither argument has a type.
We now have an overload resolution problem; we must first eliminate all the inapplicable operators, and then determine the best of the applicable operators.
Clearly the operators defined on all the non-nullable value types are inapplicable. That leaves the operators on the nullable value types, and string, and object.
We can now eliminate some for reasons of "betterness". The better operator is the one with the more specific types. int? is more specific than any of the other nullable numeric types, so all of those are eliminated. String is more specific than object, so object is eliminated.
That leaves equality operators for string, int? and bool? as the applicable operators. Which one is the best? None of them is better than the other. Therefore this should be an ambiguity error.
For this behaviour to be justified by the spec we are going to have to emend the specification to note that "null == null" is defined as having the semantics of string equality, and that it is the compile-time constant true.
I actually just discovered this fact yesterday; how odd that you should ask about it.
To answer the questions posed in other answers about why
null >= null
gives a warning about comparisons to int? -- well, apply the same analysis as I just did. The>=
operators on non-nullable value types are inapplicable, and of the ones that are left, the operator on int? is the best. There is no ambiguity error for>=
because there is no>=
operator defined on bool? or string. The compiler is correctly analyzing the operator as being comparison of two nullable ints.To answer the more general question about why operators on null values (as opposed to literals) have a particular unusual behaviour, see my answer to the duplicate question. It clearly explains the design criteria that justify this decision. In short: operations on null should have the semantics of operations on "I don't know". Is a quantity you don't know greater than or equal to another quantity you don't know? The only sensible answer is "I don't know!" But we need to turn that into a bool, and the sensible bool is "false". But when comparing for equality, most people think that null should be equal to null even though comparing two things that you don't know for equality should also result in "I don't know". This design decision is the result of trading off many undesirable outcomes against one another to find the least bad one that makes the feature work; it does make the language somewhat inconsistent, I agree.
They seem to be mixing paradigms from C and SQL.
In the context of nullable variables, null == null should really yield false since equality makes no sense if neither value is known, however doing that for C# as a whole would cause issues when comparing references.
This behaviour is defined in the C# specification (ECMA-334) in section 14.2.7 (I have highlighted the relevant part):
In particular, this means that the usual laws of relations don't hold;
x >= y
does not imply!(x < y)
.Gory details
Some people have asked why the compiler decides that this is a lifted operator for
int?
in the first place. Let's have a look. :)We start with 14.2.4, 'Binary operator overload resolution'. This details the steps to follow.
First, the user-defined operators are examined for suitability. This is done by examining the operators defined by the types on each side of
>=
... which raises the question of what the type ofnull
is! Thenull
literal actually doesn't have any type until given one, it's simply the "null literal". By following the directions under 14.2.5 we discover there are no operators suitable here, since the null literal doesn't define any operators.This step instructs us to examine the set of predefined operators for suitability. (Enums are also excluded by this section, since neither side is an enum type.) The relevant predefined operators are listed in sections 14.9.1 to 14.9.3, and they are all operators upon primitive numeric types, along with the lifted versions of these operators (note that
string
s operators are not included here).Finally, we must perform overload resolution using these operators and the rules in 14.4.2.
Actually performing this resolution would be extremely tedious, but luckily there is a shortcut. Under 14.2.6 there is an informative example given of the results of overload resolution, which states:
Since both sides are
null
we can immediately throw out all unlifted operators. This leaves us with the lifted numeric operators on all primitive numeric types.Then, using the previous information, we select the first of the operators for which an implicit conversion exists. Since the null literal is implicitly convertible to a nullable type, and a nullable type exists for
int
, we select the first operator from the list, which isint? >= int?
.The compiler is inferring that in the case of the comparison operators, the
null
is being implicitly typed asint?
.Visual Studio offers a warning:
This can be verified with the following code:
Outputs:
Why?
This seems logical to me. First of all, here's relevant sections of the C# 4.0 Spec.
The null literal §2.4.4.6:
Binary numeric promotions §7.3.6.2:
Lifted operators §7.3.7:
The null-literal alone doesn't really have a type. It is inferred by what it is assigned to. No assignment takes place here however. Only considering built-in types with language support (ones with keywords),
object
or any nullable would be a good candidate. Howeverobject
isn't comparable so it gets ruled out. That leaves nullable types good candidates. But which type? Since neither left nor right operands has a specified type, they are converted to a (nullable)int
by default. Since both nullable values are null, it returns false.This question was a Deja vu, oh wait here it is...
Why does >= return false when == returns true for null values?
The thing I remember from the other answer was :
When I run
I get a warning saying:
I wonder why it is casted to an int though.