0Pricing
C# Academy · Lesson

Null-Coalescing Operators

Use ?? and ??= to provide fallbacks.

Null-Coalescing Operators is a free C# Academy lesson on CoddyKit — lesson 3 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.

The ?? Operator

The null-coalescing operator ?? returns its left operand if it is not null, otherwise the right operand. It is a compact fallback.

using System;

class Program {
    static void Main() {
        int? maybe = null;
        int value = maybe ?? -1;
        Console.WriteLine(value);
    }
}

?? Picks the First Non-Null

If the left side has a value, that value wins and the right side is ignored.

using System;

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

?? with Reference Types

?? also works for reference types like string, giving a default text when null.

using System;

class Program {
    static void Main() {
        string name = null;
        Console.WriteLine(name ?? "Guest");
    }
}

Chaining ??

You can chain several fallbacks; the first non-null one is used.

using System;

class Program {
    static void Main() {
        string a = null, c = "third";
        string b = null;
        Console.WriteLine(a ?? b ?? c ?? "last");
    }
}

?? Returns the Underlying Type

When the left is int? and the right is int, the result is a plain int, since null can no longer happen.

using System;

class Program {
    static void Main() {
        int? maybe = null;
        int sure = maybe ?? 0;
        Console.WriteLine(sure + 5);
    }
}

The ??= Operator

The null-coalescing assignment ??= assigns the right side only if the left is currently null.

using System;

class Program {
    static void Main() {
        int? count = null;
        count ??= 10;
        Console.WriteLine(count);
    }
}

??= Leaves Existing Values

If the variable already has a value, ??= does nothing.

using System;

class Program {
    static void Main() {
        string config = "custom";
        config ??= "default";
        Console.WriteLine(config);
    }
}

Lazy Initialization with ??=

??= is perfect for setting a value the first time and keeping it afterward.

using System;

class Program {
    static string cache = null;
    static string GetData() {
        cache ??= "loaded";
        return cache;
    }
    static void Main() {
        Console.WriteLine(GetData());
        Console.WriteLine(GetData());
    }
}

?? vs GetValueOrDefault

Both supply fallbacks, but ?? is more flexible: it works with reference types and lets the fallback be any expression.

using System;

class Program {
    static void Main() {
        int? n = null;
        Console.WriteLine(n ?? 5);
        Console.WriteLine(n.GetValueOrDefault(5));
    }
}

?? in Method Arguments

Use ?? inline to provide a default right where a value is needed.

using System;

class Program {
    static void Greet(string name) {
        Console.WriteLine("Hello, " + name);
    }
    static void Main() {
        string input = null;
        Greet(input ?? "friend");
    }
}

Short-Circuit Behavior

The right side of ?? is evaluated only when the left is null, similar to short-circuiting.

using System;

class Program {
    static int Compute() {
        Console.WriteLine("Computed");
        return 42;
    }
    static void Main() {
        int? known = 1;
        int result = known ?? Compute();
        Console.WriteLine(result);
    }
}

Quick Check

Test your understanding of the null-coalescing operators.

Recap

The ?? operator returns the left value if it is not null, otherwise the right one, and it can be chained. The result type drops nullability when a non-null fallback is supplied.

??= assigns only when the target is null, making it ideal for defaults and lazy initialization. Both evaluate the right side only when needed.

Frequently asked questions

Is the “Null-Coalescing Operators” lesson free?

Yes — the full text of “Null-Coalescing Operators” 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 “Null-Coalescing Operators”?

Use ?? and ??= to provide fallbacks. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Null-Coalescing Operators” 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