O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required