Null coalescing operator
The null coalescing operator (or Logical Defined Or Operator) is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, including C#,[1] and Perl as of version 5.10.[2]
Contents |
[edit] C#
In C#, the null coalescing operator is ??. It is most often used to simplify null expressions as follows:
possiblyNullValue ?? valueIfNull
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:
string pageTitle = suppliedTitle ?? "Default Title";
instead of the more verbose
string pageTitle = (suppliedTitle == null) ? "Default Title" : suppliedTitle;
or
string pageTitle; if (suppliedTitle == null) pageTitle = "Default Title"; else pageTitle = suppliedTitle;
The three forms are logically equivalent.
The operator can also be used multiple times in the same expression:
return some_Value ?? some_Value2 ?? some_Value3;
Once a non null value is assigned to number, the expression is completed.
[edit] Perl
In Perl (starting with version 5.10), the operator is // and the equivalent Perl code is:
$possibly_null_value // $value_if_null
The possibly_null_value is evaluated as null or not-null (or, in Perl, undefined or defined). On the basis of the evaluation, the expression returns either value_if_null when possibly_null_value is null, or possibly_null_value otherwise. This is similar to the way ternary operators (?: statements) work in languages that support them. The above Perl code is equivalent to the use of the ternary operator below:
defined($possibly_null_value) ? $possibly_null_value : $value_if_null
This operator's most common usage is to minimize the amount of code used for a simple null check.
In Perl, the simpler expression below will often produce the same result, since the logical || will evaluate to the first true expression:
$possibly_null_value || $value_if_null
However, this does not distinguish between an undefined value, and other "false" values such as 0 and the empty string, so can only be used safely if you know that $possibly_null_value, if defined, will be a "true" value.
[edit] SQL
In Oracle's PL/SQL, the NVL() function provides the same outcome:
NVL(possibly_null_value, 'value if null');
In SQL Server/Transact-SQL there is the ISNULL function the follows the same prototype pattern:
ISNULL(possibly_null_value, 'value if null');
Attention should be taken for not confusing ISNULL with IS NULL - the last serves to evaluate whether some contents is defined to be NULL or not.
[edit] Python
In Python the "or" operator can be used to achieve similar behavior to the ?? operator in C#. It should be noted that the Python "or" is actually a Boolean operator. Due to the fact that any object that is not NoneType behaves as true, Python allows the or operator to return the first non NoneType object in the expression making it semantically identical to a null coalescing operator.
foo = None bar = foo or 1 #The value of bar is now 1
[edit] Scheme
In Scheme, "boolean false" and "null" are represented by the same value, written as #f. Furthermore, #f is the only value in Scheme that is treated as false by boolean operaters, unlike some other languages, in which values like 0, the empty string, and the empty list may function as false. The boolean 'or' operation, written as (or x y), returns x if it is not false, otherwise it returns y. Thus, in Scheme, there is no need for a separate "null coalescing operator", because the or operator serves that purpose.