Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetType on Nullable Boolean

Tags:

c#

null

boolean

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?

like image 526
Daniel Avatar asked Jul 24 '16 11:07

Daniel


People also ask

What is nullable Boolean?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.

Can Boolean be null in C#?

C# has two different categories of types: value types and reference types. Amongst other, more important distinctions, value types, such as bool or int, cannot contain null values.

Which statement is true about nullable type?

Characteristics of Nullable Types Nullable types can only be used with value types. The Value property will throw an InvalidOperationException if value is null; otherwise it will return the value. The HasValue property returns true if the variable contains a value, or false if it is null. You can only use == and !=

What is GetType () name in C#?

GetType or Assembly. GetTypes method to get Type objects. If a type is in an assembly known to your program at compile time, it is more efficient to use typeof in C# or the GetType operator in Visual Basic. If typeName cannot be found, the call to the GetType(String) method returns null .


1 Answers

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>
like image 186
Zein Makki Avatar answered Sep 30 '22 07:09

Zein Makki