Backing Fields- Nullable type

Nullable Type


  • Allow definition of a value OR null
a nullable type is a value type, such as integer, decimal or date, that allows definition of value or null. 
  • Specified with a "?" suffix on the type
A nullable type is specified by adding a question mark as the suffix on the type.
  • Distinguishes "not set" from the value type.
A nullable type is useful if the code needs to distinguish between not set and the type's default value.

for example, if we declared the cost as a decimal value and that value was 0, does that mean that the cost was not yet set?
or that the cost was set and the actual cost is 0?
 A nullable type helps us makes that distinction.


Nullable Type Best Practices

Do:  

  • use on simple types to distinguish "not set" and "default value".
  • Use properties of the type such as HasValue and Value as needed.



Avoid:

  • using them if they are not necessary


Comments

Popular posts from this blog

Constant vs Read-Only

Constants

FAQ: