Описание тега null-coalescing
The concept of setting a default value if a condition evaluates to null.
In C#, the ??
operator is known as the null coalescing operator. It returns the first operand unless it's null, in which case it returns the second operand:
string foo = null;
Console.WriteLine(foo ?? "bar"); // Outputs "bar"
foo = "baz";
Console.WriteLine(foo ?? "bar"); // Outputs "baz"
There are a few equivalent ideas in other languages but not all languages have an equivalent operator. As of Perl 5.10, one can use the //
operator to set a default value if the condition evaluates to undef
.