I was looking into nullable bools when I found this article on Microsoft MSDN
How to: Identify a Nullable Type (C# Programming Guide)
You can use the C# typeof operator to create a Type object that represents a Nullable type.
So I tried checking with a nullable bool:
Console.Write(typeof(bool?)); //System.Nullable`1[System.Boolean]
The article on MSDN says
You can also use the classes and methods of the System.Reflection namespace to generate Type objects that represent Nullable types. However, if you try to obtain type information from Nullable variables at runtime by using the GetType method or the is operator, the result is a Type object that represents the underlying type, not the Nullable type itself.
Calling GetType on a Nullable type causes a boxing operation to be performed when the type is implicitly converted to Object. Therefore GetType always returns a Type object that represents the underlying type, not the Nullable type.
If this is true I expect to get the same result from .GetType()
whether I use a nullable bool or a regular bool. But this is not what happens:
bool a = new bool();
Console.Write(a.GetType()); //Prints System.Boolean
bool? b = new bool?();
Console.Write(b.GetType()); //Exception!
The exception that occured:
An unhandled exception of type 'System.NullReferenceException' occurred in BoolTest.exe
Additional information: Object reference not set to an instance of an object.
But the object reference is set to an instance of an object. What could be the cause of this error?
You're calling GetType
on a NULL
Reference (The result of boxing a Nullable Type with No Value).
bool? b = new bool?();
is equivalent to bool? b = null;
Try this to get the correct result:
bool? b = new bool?(false);
Console.Write(b.GetType()); // System.Boolean
The documentation means that if you call GetType()
successfully on a Nullable
object that has value (Not Null). You get the Underlying type which is System.Boolean
. But you can't call any method using a NULL
reference and this is a general rule that applying to any reference type.
To clear the equivalence point between = null
and new bool?()
, check this Fiddle. Both generates the same IL:
IL_0001: ldloca.s V_0
IL_0003: initobj valuetype [mscorlib]System.Nullable`1<bool>
and
IL_0009: ldloca.s V_1
IL_000b: initobj valuetype [mscorlib]System.Nullable`1<bool>
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));