why is typeof int? an Int32
int? x = 1;
Console.WriteLine(x.GetType().Name);
If it is okay then what’s the use of Nullable.GetUnderlyingType?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Calling
GetType()boxes your variable. The CLR has a special rule thatNullable<T>gets boxed toT. Sox.GetTypewill returnInt32instead ofNullable<Int32>.Since a
Nullablecontainingnullwill be boxed tonullthe following will throw an exception:To quote MSDN on Boxing Nullable Types: