Possible Duplicate:
Nullable type is not a nullable type?
In the following code:
DateTime? dt = DateTime.Now;
MessageBox.Show(dt.GetType().ToString());
the message box shows “System.DateTime”, instead of Nullable<DateTime>. The following also returns false (because the GetType is wrong):
if (dt.GetType().IsAssignableFrom(typeof(DateTime?)))
...
(btw, using DateTime? or Nullable<DateTime> doesn’t make a difference)
In the watch window, you have the “Type” column that’s displaying the correct type (System.DateTime?).
In my code I have reference to dt as an object, so I need to get to the underlying type correctly. How can I do this?
Quoting MSDN (How to: Identify a Nullable Type):
so basically your code is equal to:
also, looking at “Boxing Nullable Types” on MSDN we read:
this clearly explain the “strange” behavior of
Nullable<T>.GetType()