Welcome to Shaun Luttin's public notebook. It contains rough, practical notes. The guiding idea is that, despite what marketing tells us, there are no experts at anything. Sharing our half-baked ideas helps everyone. We're all just muddling thru. Find out more about our work at bigfont.ca.

C# Nullable Type

Tags: c#, .NET, null

How does it work?

  • it is a struct that wraps a struct
  • if the underlying struct has a value, then
    • HasValue is true and
    • Value returns the value.
  • if the underlying struct lacks a value, then
    • HasValue is false and
    • Value throws an InvalidOperationException.
  • 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.


  • 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