C# Frequently Asked Questions

The C# team posts answers to common questions

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

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.

Published Tuesday, May 25, 2004 11:51 AM by CSharpFAQ
Filed under:

Comments

 

Virag said:

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.
May 27, 2004 1:11 AM
 

Klaus RM said:

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 ;-)
May 27, 2004 6:37 AM
 

Kael Rowan said:

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?
May 27, 2004 12:08 PM
 

Talbott Crowell said:

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.
May 28, 2004 9:18 PM
 

Kael Rowan said:

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."
May 31, 2004 7:33 AM
 

Panopticon Central said:

Nothing doing.
June 4, 2004 1:14 PM
 

Flier's Sky said:

Nullable types
July 7, 2004 11:31 PM
 

random ramblings.... said:

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?...
March 18, 2006 1:39 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker