Why don't nullable relational operators return bool? instead of bool?

Why don't nullable relational operators return bool? instead of bool?

  • Comments 12

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.

Leave a Comment
  • Please add 2 and 6 and type the answer here:
  • Post
  • I think the above is applicable to C# 2.0 It will be better if the version to which the faq is applicable is mentioned because "int?" and "nullable bool" sent me googling for them and I spent some real worried moments trying to figure out that its a new thing and its not a concept/feature in the existing C# version.
  • Do you know where to come with an opinion on the new syntax? I hate it. I ruins one of my best selling points for C#: The syntax is simple and readable.

    Now we can have "??" and "int?". Well, this doesn't help to make the code readable.

    On the other hand maybe obfuscators is not needed any more ;-)
  • Is this sort of question-mark syntax in the 2.0 specification or is it for a future version of the language, such as C# 3.0? Is the question mark a shortcut for the Nullable generic?
  • I am very glad that you decided that the relational operators will return a bool (true/false). If relational operators returned a tri-state value (true/false/null) it would make the language unbearable.
  • I found the answer to my question in section 24.1 of the C# 2.0 Spec:

    "The syntax T? is shorthand for System.Nullable<T>, and the two forms can be used interchangeably."
  • Nothing doing.
  • Nullable types
  • Reminder to self for when I inevitably loose the bookmarks when my computer goes on a bookmark eating frenzy: -- Nullable Types in C# -- The Truth about Nullable Types and VB... -- Why don't nullable 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.

Page 1 of 1 (12 items)