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

Published 25 May 04 11:51 AM

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.

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Virag said on May 27, 2004 1:11 AM:
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.
# Klaus RM said on May 27, 2004 6:37 AM:
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 ;-)
# Kael Rowan said on May 27, 2004 12:08 PM:
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?
# Talbott Crowell said on May 28, 2004 9:18 PM:
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.
# Kael Rowan said on May 31, 2004 7:33 AM:
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."
# Panopticon Central said on June 4, 2004 1:14 PM:
Nothing doing.
# Flier's Sky said on July 7, 2004 11:31 PM:
Nullable types
# random ramblings.... said on March 18, 2006 1:39 PM:
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?...

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker