Arithmetic with Nullables

What about nullable value types? How does arithmetic deal with the magical null value? The answer lies in the concept of lifted operators, which are operators that “lift” the use of operators that normally deal with non-nullable types to use with nullables:

+  ++  -  --  !  ~+  -  *  /  %  &  |  ^  <<  >>==  !=  <  >  <=  >=

Don’t worry about operators that don’t look familiar yet; we’ll get to those soon. The specification of lifted operators defines what it means to apply the operator to a number of null values. For example, for the binary arithmetic operations, the result is null if any of the operands was null:

int? a = null;int b = 5;int? res = a + b;

Let’s not go into too much detail on this subject because things ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.