0Pricing
C# Academy · Lesson

Nullable in Calculations

Understand how nulls propagate through arithmetic.

Nullable in Calculations is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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);
    }
}

Printing Null Results

A null arithmetic result prints as an empty string, matching how nullables display.

using System;

class Program {
    static void Main() {
        int? a = null;
        Console.WriteLine("Result: [" + (a * 2) + "]");
    }
}

Mixing Nullable and Non-Nullable

You can mix int and int? in math; the int is promoted to nullable, and the result is nullable.

using System;

class Program {
    static void Main() {
        int? a = 10;
        int b = 4;
        int? result = a - b;
        Console.WriteLine(result);
    }
}

Lifted Comparisons

Comparisons on nullables also lift. If either side is null, ordering comparisons like < return false.

using System;

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

Equality with Null

Unlike ordering, equality is defined: two nulls are equal, and null is not equal to any number.

using System;

class Program {
    static void Main() {
        int? a = null;
        int? b = null;
        Console.WriteLine(a == b);
        Console.WriteLine(a == 0);
    }
}

Combining ?? with Math

Use ?? to replace null with a safe number before computing, keeping the result non-null.

using System;

class Program {
    static void Main() {
        int? a = null;
        int total = (a ?? 0) + 100;
        Console.WriteLine(total);
    }
}

Guarding Division

Combine HasValue checks with calculations to avoid both nulls and division by zero.

using System;

class Program {
    static void Main() {
        int? a = 20, b = 4;
        if (a.HasValue && b.HasValue && b.Value != 0)
            Console.WriteLine(a.Value / b.Value);
    }
}

Summing Nullable Data

When summing data that may contain nulls, decide whether to skip or treat null as zero.

using System;

class Program {
    static void Main() {
        int?[] data = { 5, null, 10, null, 2 };
        int total = 0;
        foreach (var x in data)
            total += x ?? 0;
        Console.WriteLine(total);
    }
}

Nullable Bool in Logic

A bool? participates in lifted logic: true | null is true, false & null is false, otherwise null.

using System;

class Program {
    static void Main() {
        bool? a = null;
        bool? b = true;
        Console.WriteLine(a | b);
    }
}

Converting Result to Plain

After a nullable calculation, use ?? or GetValueOrDefault to land on a plain value for display.

using System;

class Program {
    static void Main() {
        int? a = 6, b = 7;
        int product = (a * b) ?? 0;
        Console.WriteLine(product);
    }
}

Quick Check

Test your knowledge of nullable arithmetic.

Recap

C# lifts arithmetic, comparison, and logical operators to nullable types. Any null operand in arithmetic yields a null result, and ordering comparisons against null return false while equality is well defined.

Use ?? or GetValueOrDefault to substitute safe values before or after calculations, and guard with HasValue when needed.

Frequently asked questions

Is the “Nullable in Calculations” lesson free?

Yes — the full text of “Nullable in Calculations” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Nullable in Calculations”?

Understand how nulls propagate through arithmetic. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Nullable in Calculations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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