No, I’m not confused. Read on and all shall become clear…
Since the dawn of time*, the conditional-expression operator, ?:, has confused C-language newbies for generations. Not to be outdone, C# 2.0 has introduced the ?? operator. This is a little known addition to the C# language that has an interesting use – besides confusing VB developers, that is. Consider the following code:
if(title != null) {
return title;
} else {
return string.Empty;
}
Basically you want to return a default value if your reference is null. If your type happens to be a string, this could be a shared instance (such as string.Empty) or “Default Value” or something else. Other reference types (or Nullables) could do something else.
This isn’t hard code, but it is a fair amount of typing. So many would shorten it to:
return (title != null) ? title : string.Empty;
Checking for null returns is fairly common, especially in database work or when reading from config files. So C# 2.0 introduced the ?? operator:
return title ?? string.Empty;
This does exactly the same thing as the previous two examples, just with less typing. Another interesting use is when working with Nullables:
int? x = ReadFromConfig();
// Do some work
int y = x ?? 42;
It will take awhile to get used to, but is an interesting addition to the C# toolbox.
* The dawn of time is roughly sometime in the early 1970s.