Your actual question seems to be:
By initializing a Nullable<T>
to its default value using its default constructor, for example bool? b = new bool?();
, why does accessing b
's members such as GetType()
throw a NullReferenceException
? Doesn't new
always return a value, so b
can't really be null
?
Well, yes and no. Nullable<T>
is kind of special. From the C# specs:
4.1.10 Nullable types
[...]
An instance of a nullable type T? has two public read-only properties:
- A HasValue property of type bool
- A Value property of type T
An instance for which HasValue is true is said to be non-null. A non-null instance contains a known value and Value returns that value.
An instance for which HasValue is false is said to be null. A null instance has an undefined value. Attempting to read the Value of a null instance causes a System.InvalidOperationException to be thrown.
So yes, bool? b = new bool?();
does return an instance: one you only can call HasValue
on. Since it returns false
, you can't do much else with that instance.
Then the next relevant section:
4.3.1 Boxing conversions
Boxing a value of a nullable-type produces a null reference if it is the null value (HasValue is false)
This is also explained in MSDN: Boxing Nullable Types (C# Programming Guide):
Objects based on nullable types are only boxed if the object is non-null. If HasValue is false, the object reference is assigned to null instead of boxing.
Somewhat further into the specs:
11.3.5 Boxing and unboxing
When a struct type overrides a virtual method inherited from System.Object (such as Equals, GetHashCode, or ToString), invocation of the virtual method through an instance of the struct type does not cause boxing to occur.
GetType()
is not overridden by Nullable<T>
, so boxing will occur. When you call GetType()
or any non-overridden method on a struct, the struct will be boxed to an object before calling that method. In the case of a null Nullable<T>
, the result of that boxing operation will be (object)null
. Hence the exception.
See also Does calling a method on a value type result in boxing in .NET?.
So, to answer your question:
b
is not null, it holds a Nullable<bool>
with a HasValue
indicating false
.
- Calling the non-overridden
GetType()
method on it, causes the Nullable<bool>
struct to be boxed in order to access the underlying method object.GetType()
.
- This boxing operation actually doesn't box, but simply returns
(object)null
.
- In the end, you're calling
((object)null).GetType()
, which throws the NullReferenceException
you encounter.
If you're actually looking for a piece of code that can return the type of any variable, even a null Nullable<T>
, use something like this:
Type GetType<T>(T obj)
{
return typeof(T);
}
Which you can then call like this:
Console.WriteLine(GetType(b));