Is there any difference between type? and Nullable<type>?

C#.NetNullable

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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

Solution 3 - C#

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;
}

Solution 4 - C#

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.)

Solution 5 - C#

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>".

Solution 6 - C#

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.

Solution 7 - C#

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.

Solution 8 - C#

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

Solution 9 - C#

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."

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMojoFilterView Question on Stackoverflow
Solution 1 - C#Steve MorganView Answer on Stackoverflow
Solution 2 - C#samjudsonView Answer on Stackoverflow
Solution 3 - C#Mark IngramView Answer on Stackoverflow
Solution 4 - C#svickView Answer on Stackoverflow
Solution 5 - C#Curt HagenlocherView Answer on Stackoverflow
Solution 6 - C#spoulsonView Answer on Stackoverflow
Solution 7 - C#morechilliView Answer on Stackoverflow
Solution 8 - C#user1228View Answer on Stackoverflow
Solution 9 - C#AMCView Answer on Stackoverflow