Partager via


?? Operator

The ?? operator is used to return the left operand if it's not null, otherwise, it returns the right operand. It's equivalent to (left != null ? left : right).

string a = null;
object b = a ?? new object();
object c = b ?? "b is null!";
int? x = null;
int y;

// These statements should be equivalent:

y = x ?? default(int);
y = x.GetValueOrDefault();
y = x.HasValue ? x.Value : default(int);
y = x != null ? x.Value : default(int);

For more info, please read: ?? Operator (C# Reference) and default Keyword in Generic Code (C# Programming Guide)