C# Academy · Lesson

Introducing Nullable Value Types

Declare int?, double? and understand Nullable .

Lesson 1 of 413 steps

Introducing Nullable Value Types is a free C# Academy lesson on CoddyKit. This is lesson 1 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, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.

Value Types Cannot Be Null

Normally an int always holds a number; it can never be null. Sometimes you need to represent "no value", such as a missing measurement.

using System;

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

Nullable with ?

Add ? after a value type to make it nullable. int? can hold any int or null.

using System;

class Program {
    static void Main() {
        int? maybe = null;
        Console.WriteLine(maybe.HasValue);
        maybe = 42;
        Console.WriteLine(maybe);
    }
}

Other Nullable Types

Any value type can be nullable: double?, bool?, DateTime?, and so on.

using System;

class Program {
    static void Main() {
        double? temp = null;
        bool? flag = true;
        Console.WriteLine(temp.HasValue + " " + flag);
    }
}

Nullable<T> Spelled Out

int? is just shorthand for the struct Nullable<int>. Both compile to exactly the same type.

using System;

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

Assigning Null

You can freely assign null to a nullable variable and reassign a real value later.

using System;

class Program {
    static void Main() {
        int? id = 100;
        Console.WriteLine(id);
        id = null;
        Console.WriteLine(id.HasValue);
    }
}

Printing a Null Nullable

Printing a nullable that is null produces an empty string, not the word null.

using System;

class Program {
    static void Main() {
        int? value = null;
        Console.WriteLine("[" + value + "]");
    }
}

Implicit Conversion to Nullable

A plain int converts to int? automatically because every int is a valid nullable int.

using System;

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

Nullable in Method Returns

A method can return a nullable type to signal "found a value" versus "found nothing".

using System;

class Program {
    static int? FindEven(int[] nums) {
        foreach (int n in nums)
            if (n % 2 == 0) return n;
        return null;
    }
    static void Main() {
        Console.WriteLine(FindEven(new[] {1, 3, 4}));
    }
}

Comparing Nullables

Equality works as expected: two nullables are equal if both are null or both hold the same value.

using System;

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

Why Use Nullable Types

Nullable value types model optional data cleanly, such as a database column that allows NULL or a field a user left blank.

using System;

class Program {
    static void Main() {
        int? userAge = null;
        string msg = userAge.HasValue ? "Age set" : "Age unknown";
        Console.WriteLine(msg);
    }
}

Nullable Is a Struct

Nullable<T> itself is a value type, so a nullable variable is never a reference even though it can be null.

using System;

class Program {
    static void Main() {
        int? n = 3;
        Console.WriteLine(n.GetType().Name);
    }
}

Quick Check

Test your understanding of nullable value types.

Recap

Value types like int cannot be null by default. Adding ? creates a nullable type (int?), which is shorthand for Nullable<T>.

Nullable types let you represent missing or optional data, convert implicitly from the underlying type, and are themselves structs.

Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

Frequently Asked Questions

Is the “Introducing Nullable Value Types” lesson free?

Yes — the full text of “Introducing Nullable Value Types” is free to read here on the web. 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. The C# Academy course includes 4 lessons in total.

What will I learn in “Introducing Nullable Value Types”?

Declare int?, double? and understand Nullable . 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Introducing Nullable Value Types” 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