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# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.