If you look at the documentation for the .NET Nullable, you'll see:
struct Nullable<T>
Note that it's a struct, not a class.
It seems a struct Nullable<T>
is not a ValueType, which is very unexpected. The following code prints False:
Type nullableType = typeof(Nullable<int>);
Console.WriteLine(nullableType is ValueType);
If you look at the generated IL you'll see that that compiler determined nullableType
to be a ValueType at compile time. But how can it be a ValueType if it's a struct? All structs are ValueTypes, right? Obviously, it has something to do with the generic.
What am I missing here? Is there something in the language spec about this?
Thanks.
You are testing if System.Type
is a value type. It is not. Rewrite it like this:
Nullable<int> test = 42;
Console.WriteLine(test is ValueType);
It is a value type, try it this way:
Type nullableType = typeof(Nullable<int>);
Console.WriteLine(nullableType.IsValueType);
That should return true. Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With