B
Ben Voigt [C++ MVP]
Well, you have to do that or compare to null with either the cast or
No. The Value property will throw an exception when HasValue is false.
Anyway, the Value property is the base (non-nullable) type, which can never
be null, so comparison with null is impossible. Same for the result of the
cast.
You must check HasValue before casting or accessing the Value property. My
personal preference (for efficiency) is to check HasValue, then call
GetValueOrDefault() which does not throw, it is just a simple field access.
using the Value property.
No. The Value property will throw an exception when HasValue is false.
Anyway, the Value property is the base (non-nullable) type, which can never
be null, so comparison with null is impossible. Same for the result of the
cast.
You must check HasValue before casting or accessing the Value property. My
personal preference (for efficiency) is to check HasValue, then call
GetValueOrDefault() which does not throw, it is just a simple field access.