Small confusion in Nullable Type
-
Declaring bool? b = true; Then, b.GetType(); should return Nullable or Null? Why does it has type bool? I am not clear on this. Your know' will be helpful. :)
From http://msdn.microsoft.com/en-us/library/ms366789%28VS.80%29.aspx[^]: However, if you attempt to obtain type information from Nullable variables at runtime 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.
-
Declaring bool? b = true; Then, b.GetType(); should return Nullable or Null? Why does it has type bool? I am not clear on this. Your know' will be helpful. :)
RaviSant wrote:
should return Nullable or Null?
return Null See these links will help you to get idea. Nullable Types[^] Using Nullable Types[^]
-
Declaring bool? b = true; Then, b.GetType(); should return Nullable or Null? Why does it has type bool? I am not clear on this. Your know' will be helpful. :)
RaviSant wrote:
b.GetType();
It will return underlying datatype only.
RaviSant wrote:
should return Nullable or Null?
No. It's just prone to accept null values that's it.
Regards, Hiren.
My Recent Article: - Way to know which control have raised a postback
My Recent Tip/Trick: - Remove HTML Tag, get plain Text -
From http://msdn.microsoft.com/en-us/library/ms366789%28VS.80%29.aspx[^]: However, if you attempt to obtain type information from Nullable variables at runtime 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.
Excellent answer. Clear, concise and accurate - just the kind I like.:thumbsup:
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
From http://msdn.microsoft.com/en-us/library/ms366789%28VS.80%29.aspx[^]: However, if you attempt to obtain type information from Nullable variables at runtime 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.
-
Declaring bool? b = true; Then, b.GetType(); should return Nullable or Null? Why does it has type bool? I am not clear on this. Your know' will be helpful. :)
RaviSant wrote:
Declaring bool? b = true;
The intention of nullable types is to allow/accept null value. So when we declare "bool?" then, it is of type "Boolean" and can accept null value. You can check if the variable has the value by using "HasValue" property of variable. if it is true then get the value of variable using "Value" property.
Knock out 't' from can't, you can if you think you can. :cool: