C# - What is nullable type Unwrapping?

Unwrapping is accessing the Value property of a nullable type.

Unwrapping examples:


 
  // a wrapped value
  int? val = 10;
 
  // unwrapping (five different ways)
  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