Using the Null-Coalescing (??) Operator with Custom Nullable Types

In addition to the built-in nullable types, you can also use the null-coalescing operator on custom nullable types usingNullable<T>.
Here’s an example:


1
2
3
4
5
Nullable<Mood> myMood1 = null;
Nullable<Mood> myMood2 = Mood.Petulant;
 
Mood mood3 = myMood1 ?? Mood.Happy;   // result = Happy
Mood mood4 = myMood2 ?? Mood.Happy;   // result = Petulant