- ?? Operator
The
??
operator, sometimes called the Coalescing Operator, is a binary operator that is part of the syntax for a basic conditional expression in severalprogramming language s, most notably C#.Conditional assignment
??
is most frequently used to simplify null expressions as follows:"possibly_null_value" ?? "value_if_null"
The "possibly_null_value" is evaluated "true" or "false" as a
Boolean expression . On the basis of the evaluation of the Boolean condition, the entire expression returns "value_if_null" when "possibly_null_value" is null, but "possibly_null_value" otherwise. This is similar to the wayternary operators (
statements) work in?: functional programming languages.This operator's most common usage is to minimize the amount of code used for a simple null check. For example, if we wish to implement some C# code to give a page a default title if none is present, we may use the following statement:
instead of the more verboseorThe three forms are equivalent.
JavaScript Implementation
In JavaScript, the equivalent of this is:
ee also
*
?:
*C#External links
* [http://blog.devstone.com/Aaron/archive/2006/01/02/1404.aspx New Operator in C# 2.0: ?? (null coalescing operator)]
Wikimedia Foundation. 2010.