C# - What is nullable type Unwrapping?

The process of accessing the Value property of a nullable type is called unwrapping.

Unwrapping:


  int i1 = (int)val;                  // Returns val.Value or throws a System.InvalidOperationException
  int i2 = val.Value;                 // Returns val.Value or throws a System.InvalidOperationException
  int i3 = val.GetValueOrDefault();   // Returns val.Value or zero
  int i4 = val.GetValueOrDefault(-1); // Returns val.Value or -1
  int i5 = val ?? -1;                 // Returns val.Value or -1
  

Ads by Google

Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath