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?

- 60,845
- 93
- 320
- 589

- 12,256
- 14
- 53
- 61
-
2This 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
-
1I 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 Answers
If you look at the IL using Ildasm, you'll find that they both compile down to Nullable<bool>
.

- 15,374
- 13
- 103
- 121

- 12,978
- 2
- 40
- 49
-
4
-
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
There is no difference between bool? b = null
and Nullable<bool> b = null
. The ?
is just C# compiler syntax sugar.

- 56,243
- 7
- 59
- 69
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;
}

- 71,849
- 51
- 176
- 230
-
2hmm, 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
-
2If 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
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.)

- 236,525
- 50
- 385
- 514
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>
".

- 20,680
- 8
- 60
- 50
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.

- 21,335
- 15
- 77
- 102
-
Even though this comment was from way back it's possible that they felt this answer duplicated other answers so they DV'd? – Chris Marisic Feb 01 '10 at 16:17
-
Apparently I'm not the fastest gun in the west. All the top answers are within a few minutes. – spoulson Feb 05 '10 at 13:02
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.

- 9,827
- 7
- 33
- 54
Null primitives are just regular primitives wrapped in Nullable. Any appearances to the contrary are just the compiler and syntactical sugar.
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."

- 39
- 1
- 8