Is It Nullable ?

c# nullable


Sometimes, you may have a scenario whereby you need to know if a value is a value type (say ‘int’), or a nullable type (say ‘int?’).
Common sense suggests that we should easily be able to detect wether a type is nullable or not, but if you have a try, you’ll see there are no obvious answers, or many unobvious ones either.
The problem stems from the fact that if you query the type of a nullable instance it’ll cause it to be boxed, and the boxing operation means that GetType() brings back the type of the underlying valuetype – not the nullable.
However, after a bit of head-scratching, I ended up with this utility class

About the author

Dean Chalk

I am a freelance (contract) Microsoft WPF developer, specialising in challenging, high-performance user interface development. I have been working exclusively on enterprise WPF app development since 2006, and I have been a part of some of the most demanding WPF projects ever created withing the Investment Banking / trading industry.


>