C# Nullable Type
How does it work?
- it is a struct that wraps a struct
-
if the underlying struct has a value, then
-
HasValueis true and -
Valuereturns the value.
-
-
if the underlying struct lacks a value, then
-
HasValueis false and -
Valuethrows anInvalidOperationException.
-
-
GetValueOrDefault()always returns the inner struct's default value.
When is it useful?
It is useful when we do NOT want to assume that the user (or client application, or data source...) provided the default value, when in fact the user (or client app...) provided no value.
StackOverflow:
- http://softwareengineering.stackexchange.com/q/340638/116650
- http://softwareengineering.stackexchange.com/q/340638/116650
- http://stackoverflow.com/q/11146825/1108891
Reference Source:
- https://referencesource.microsoft.com/#mscorlib/system/nullable.cs,137