I have an unexpected behaviour with nullables on primitives.
My test code:
Nullable<long> value = long.Parse("5");
Type type = value.GetType();
// at this Point type is System.Int64 and not Nullable<System.Int64>
Is there any possibility, where value stays a Nullable<System.Int64> and does not get converted to a regular long?
No. You have already assigned it to a long? variable.
When you call GetType(), this function is defined on System.Object. Therefore to call it, we must box the value.
You can see this by viewing the generated MSIL instructions for the
GetType()call:
box System.Nullable<System.Int64>
call System.Object.GetType
So when it is boxed it actually pushes thelong?on the stack, and the boxing only gives us a boxedlong
Hence the resulting value, in the case of a nullable, is the base type (see here and here).
In this instance, there is no reason to call GetType anyway, because we know it's a long?
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