The C# team posts answers to common questions and describes new language features
Q: Why don't nullable relational operators return “bool?” instead of “bool“?
Nullable types are a new feature of C# 2.0.
When dealing with nullable types, if you write the following:
int? i = 15;int? j = 16;
the type of
i == j
is a normal bool, rather than a bool? (ie nullable bool). Why?
A: In our initial designs, we spent a fair amount of time discussing what the behavior should be here, and how we should cross over from the three-valued nullable world to the two-valued “normal C#“ world.
Consider the following method:
void Process(int? p){ if (p == null) { // do some processing... }}
This seems like a very natural thing for a C# user to want to write. But if equality is three-valued, comparing anything to null always returns a null value (ie null isn't equal to anything), and therefore such a comparison can never succeed. Instead, the user would have to write:
if (!p.HasValue)
or something similar. We decided that the value of having a model that was consistent with the way users were used to dealing with reference null was pretty high, and therefore decided to make the relational operators return bool.
Klaus RM: language nazi's like you will always be around. I think C# was very well thought out, and the nullable types are great and solve a great deal of problems. The ??-operator is a great help too. It's right in front of you when used, and it's totally readable.
Very bad decision.
And NaN != NaN in the same time.
The compiler should signal a warning when comapring to null (== always false and != always true).
Or you can say that Null means "no value" instead of "undefined", which is pretty weird.
In that way, Null == Null might be ok. But still Null != Null would have been better in my opinion.
So C# null is a different concept from database null.
But I guess when you test for null references, you don't think of null as undefined so you therefore expect null == null (no address == no adress).
I kinda changed my opinion. I guess null == null is better.