August 9, 2011

Null-coalescing or ?? operator

Always forgetting the name of this operator!

The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type.
// Set defaultValue to Settings.DefaultValue unless it is null 
// in which case set it to string.Empty
string defaultValue = Settings.DefaultValue ?? string.Empty;

No comments: