Q:

Equivalent to Java's Optional.orElse in C#

I'm looking for nice syntax for providing a default value in the case of null. I've been used to using Optional's instead of null in Java where API's are concerned, and was wondering if C#'s nicer nullable types have an equivalent?
Optionals
Optional<String> x = Optional<String>.absent();
String y = x.orElse("NeedToCheckforNull"); //y = NeedToCheckforNull

@nullable
String x = null;
String y = x == null ? "NeedToCheckforNull" : x ; //y = NeedToCheckforNull

How would I make the above more readable in C#?
JavaScript would allow y = x | "NeedToCheckforNull"

A:

You can use the ?? operator.
Your code will be updated to:
string x = null;
string y = x ?? "NeedToCheckforNull"; 

See: ?? Operator (C# Reference)

A:

C# has the special Nullable<T> type which can be declared with int?, decimal?, etc. These can provide a default value by using .GetValueOrDefault(), T GetValueOrDefault(T defaultValue), and the ?? opterator.
string x = null;
Console.WriteLine(x ?? "NeedToCheckforNull");

