User Tools

Site Tools


notes:csharp:nullable

Nullable Types in C#

Nullable types are represented by the Nullable<T> structure. An instance of Nullable<T> is boxed to either a null reference (if it doesn't have a value) or a boxed value of T (if it does).
The type parameter T has a value type constraint specified e.g., struct Nullable<T> where T : struct

Creating and Initializing

// Implicit conversion from T to Nullable<T> (wrapping).
int? n1 = 8;                    
Nullable<int> n2 = 8;
 
// Nullable constructors.
var n3 = new Nullable<int>();  // n3 == null
var n4 = new Nullable<int>(8); // n4 == 8
 
// All three values x1, x2, and x3 are null.
int? x1 = new int?();
int? x2 = default(int?);
int? x3 = (int?)null;

Basic Operations

int? n = 8;
 
// Explicit conversion from Nullable<T> to T (unwrapping).
int i1 = (int)n; // the same as n.Value
 
// Check if a nullable variable has a value.
if (n == null) { }
if (n.HasValue) { }
 
// Use the 'is' and 'as' operators.
if (n is Int32) { }
int? m = n as Int32?;
 
// Use Nullable<T>.GetValueOrDefault()
int? n1 = 8;
int i1 = n1.GetValueOrDefault();   // 8
int i2 = n1.GetValueOrDefault(88); // 8
 
int? n2 = null;
int i3 = n2.GetValueOrDefault();   // 0
int i4 = n2.GetValueOrDefault(88); // 88

Nullable<bool>

The truth table for bool? variables determined by the C# compiler:

bool? x;
bool? y;
x y x & y x | y x ^ y !x
true true true true false false
true false false true true false
true null null true null false
false true false true true true
false false false false false true
false null false null null true
null true null true null null
null false false null null null
null null null null null null
notes/csharp/nullable.txt · Last modified: 2016/12/13 by admin