ECMA-334: 14.2.7 Lifted operators
| C# Language Specification |
| © 2006 ECMA International |
14.2.7 Lifted operators
Lifted operators permit predefined and user-defined operators that operate on non-nullable value types to also be used with nullable forms of those types. Lifted operators are constructed from predefined and userdefined operators that meet certain requirements, as described in the following:
- For the unary operators
+ ++ - -- ! ~
a lifted form of an operator exists if the operand and result types are both non-nullable value types. The
lifted form is constructed by adding a single ? modifier to the operand and result types. The lifted operator
produces a null value if the operand is null. Otherwise, the lifted operator unwraps the operand, applies the
underlying operator, and wraps the result.
- For the binary operators
+ - * / % & | ^ << >>
a lifted form of an operator exists if the operand and result types are all non-nullable value types. The lifted
form is constructed by adding a single ? modifier to each operand and result type. The lifted operator
produces a null value if one or both operands are null (an exception being the & and | operators of the
bool? type, as described in §14.10.4). Otherwise, the lifted operator unwraps the operands, applies the
underlying operator, and wraps the result.
- For the equality operators
== !=
a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type
is bool. The lifted form is constructed by adding a single ? modifier to each operand type. The lifted
operator considers two null values equal, and a null value unequal to any non-null value. If both operands
are non-null, the lifted operator unwraps the operands and applies the underlying operator to produce the
bool result.
- For the relational operators
< > <= >=
a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type
is bool. The lifted form is constructed by adding a single ? modifier to each operand type. The lifted
operator produces the value false if one or both operands are null. Otherwise, the lifted operator unwraps
the operands and applies the underlying operator to produce the bool result.
The lifted forms of the predefined operators are themselves considered predefined operators.