0Pricing
C# Academy · Lesson

Nullable in Calculations

Understand how nulls propagate through arithmetic.

Lifted Operators

C# "lifts" arithmetic and comparison operators to work on nullable types. Operators like + automatically gain a nullable version.

using System;

class Program {
    static void Main() {
        int? a = 5;
        int? b = 3;
        Console.WriteLine(a + b);
    }
}

Null Propagates Through Math

If any operand in a lifted arithmetic expression is null, the result is null.

using System;

class Program {
    static void Main() {
        int? a = 5;
        int? b = null;
        int? sum = a + b;
        Console.WriteLine(sum.HasValue);
    }
}

All lessons in this course

  1. Introducing Nullable Value Types
  2. HasValue and Value Properties
  3. Null-Coalescing Operators
  4. Nullable in Calculations
← Back to C# Academy