#215 – Using the Null-Coalescing (??) Operator with Custom Nullable Types
January 18, 2011 Leave a comment
In addition to the built-in nullable types, you can also use the null-coalescing operator on custom nullable types using Nullable<T>.
Here’s an example:
Nullable<Mood> myMood1 = null; Nullable<Mood> myMood2 = Mood.Petulant; Mood mood3 = myMood1 ?? Mood.Happy; // result = Happy Mood mood4 = myMood2 ?? Mood.Happy; // result = Petulant