52

In C# are the nullable primitive types (i.e. bool?) just aliases for their corresponding Nullable<T> type or is there a difference between the two?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
MojoFilter
  • 12,256
  • 14
  • 53
  • 61
  • 2
    This question should be edited to clarify the name of the primitive type is "bool?" and not "bool". There are good answers below, but the question is unclear. – Chris Conway Sep 11 '08 at 13:49
  • 1
    I had a similar question regarding `int?` and `Nullable` and found this question (and its answers) to be very helpful. – Brian Driscoll Oct 13 '11 at 15:07
  • please refer http://stackoverflow.com/questions/4028830/nullableint-vs-int-is-there-any-difference/38679443#38679443 – Rajes Jul 30 '16 at 23:34

9 Answers9

67

If you look at the IL using Ildasm, you'll find that they both compile down to Nullable<bool>.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Steve Morgan
  • 12,978
  • 2
  • 40
  • 49
  • 4
    Maybe they don't know what IL or ILDasm mean. Link them? – Evan Harper Feb 24 '12 at 16:51
  • While this is true, there are some corner cases of the language, when there **is** a difference between the two. See [this answer](http://stackoverflow.com/a/24738380/1552016) (and the linked question there) for more details. – qqbenq Jul 14 '14 at 14:26
38

There is no difference between bool? b = null and Nullable<bool> b = null. The ? is just C# compiler syntax sugar.

samjudson
  • 56,243
  • 7
  • 59
  • 69
18

To access the value of the bool? you need to do the following:

bool? myValue = true;
bool hasValue = false;

if (myValue.HasValue && myValue.Value)
{
  hasValue = true;
}

Note you can't just do:

if (myValue)
{
  hasValue = true;
}
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
  • 2
    hmm, not sure whether or not to up-vote this as it doesnt answer the question but it's useful and succinct nonetheless! ;-) – rohancragg Sep 11 '08 at 13:48
  • Well, according to Joel you should up-vote if you find it useful - not neccessarily if it's the answer. But then I would say that ;) – Mark Ingram Sep 11 '08 at 15:05
  • 1
    +1, but I would use different names to make it easier to read! – JohnB Jan 14 '11 at 15:12
  • 2
    If prefer to do 'if(myValue == true)' instead. It works the same way because it will be false if myValue is null. – juharr Oct 19 '12 at 14:52
9

I'm surprised nobody went to the source (the C# spec) yet. From §4.1.10 Nullable types:

A nullable type is written T?, where T is the underlying type. This syntax is shorthand for System.Nullable<T>, and the two forms can be used interchangeably.

So, no, there isn't any difference between the two forms. (Assuming you don't have any other type called Nullable<T> in any of the namespaces you use.)

svick
  • 236,525
  • 50
  • 385
  • 514
7

A Nullable<T> is a structure consisting of a T and a bit flag indicating whether or not the T is valid. A Nullable<bool> has three possible values: true, false and null.

Edit: Ah, I missed the fact that the question mark after "bool" was actually part of the type name and not an indicator that you were asking a question :). The answer to your question, then, is "yes, the C# bool? is just an alias for Nullable<bool>".

Curt Hagenlocher
  • 20,680
  • 8
  • 60
  • 50
4

A bool is a value type, therefore it can't contain a NULL value. If you wrap any value type with Nullable<>, it will give it that ability. Moreover, access methods to the value change by additional properties HasValue and Value.

But to the question: Nullable<bool> and bool? are aliases.

spoulson
  • 21,335
  • 15
  • 77
  • 102
2

No there is no difference. In summary:

System.Boolean -> valid values : true, false

bool -> alias for System.Boolean

Nullable<bool> -> valid values : true, false, null

bool? -> alias for Nullable<bool>

Hope this helps.

morechilli
  • 9,827
  • 7
  • 33
  • 54
1

Null primitives are just regular primitives wrapped in Nullable. Any appearances to the contrary are just the compiler and syntactical sugar.

0

No difference. Take a look here: http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx

"The syntax T? is shorthand for Nullable, where T is a value type. The two forms are interchangeable."

AMC
  • 39
  • 1
  • 8