C# Academy · Lesson

Nullable Value Types

int? and the Nullable struct.

Lesson 1 of 413 steps

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.

Why Nullable Value Types?

Value types like int, bool, and DateTime always hold a value. An int defaults to 0, never "no value".

Sometimes you need to represent missing data, such as an unanswered survey field or an empty database column. Nullable value types let a value type also hold null.

Declaring with int?

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

This is the most common way you will write nullable value types in everyday C# code.

int? age = null;
int count = 5;
age = count;
System.Console.WriteLine(age);

Nullable<T> Behind the Scenes

The ? syntax is shorthand. int? is exactly the same type as Nullable<int>.

Nullable<T> is a struct that wraps a value type and adds a flag tracking whether a value is present.

Nullable<int> a = 10;
int? b = 10;
System.Console.WriteLine(a == b);

HasValue and Value

Every nullable exposes two properties. HasValue is true when a value is present, and Value returns the underlying value.

Reading Value when HasValue is false throws an InvalidOperationException, so check first.

int? score = 42;
if (score.HasValue)
{
    System.Console.WriteLine(score.Value);
}

A Complete Example

Here is a full program. It assigns a value, checks HasValue, and prints the result.

Run it to see how a nullable behaves when it actually holds data.

using System;

class Program
{
    static void Main()
    {
        int? temperature = 21;
        if (temperature.HasValue)
            Console.WriteLine("Temp: " + temperature.Value);
        else
            Console.WriteLine("No reading");
    }
}

When It Is Null

Assigning null makes HasValue return false. The else branch then runs.

This pattern is how you safely handle optional readings without crashing.

using System;

class Program
{
    static void Main()
    {
        int? temperature = null;
        if (temperature.HasValue)
            Console.WriteLine(temperature.Value);
        else
            Console.WriteLine("No reading");
    }
}

GetValueOrDefault

GetValueOrDefault() returns the value if present, otherwise the default for that type (such as 0 for int).

You can also pass your own fallback: GetValueOrDefault(100) returns 100 when null.

int? quantity = null;
int result = quantity.GetValueOrDefault(100);
System.Console.WriteLine(result);

Implicit Conversion to Nullable

A regular int converts to int? automatically, because every plain value is also a valid nullable value.

The reverse is not automatic: going from int? back to int requires an explicit cast or a fallback, since the nullable might be null.

int plain = 7;
int? wrapped = plain;
int back = (int)wrapped;
System.Console.WriteLine(back);

Nullable with Other Value Types

Any value type works: bool?, double?, DateTime?, and even custom structs.

A bool? has three states: true, false, and null, which is useful for unanswered yes or no questions.

bool? agreed = null;
double? price = 9.99;
System.Console.WriteLine(agreed.HasValue);
System.Console.WriteLine(price.Value);

Reference Types Are Already Nullable

Classes like string and arrays can already be null, so they do not need the Nullable<T> wrapper.

The ? on reference types (like string?) means something different: it is a nullable-reference annotation, not the Nullable<T> struct.

Comparing Nullables

Comparisons work, but remember null behaves specially. Two nulls are equal, and any ordered comparison involving null returns false.

So null < 5 is false, and null > 5 is also false.

int? x = null;
System.Console.WriteLine(x < 5);
System.Console.WriteLine(x == null);

Quick Check

Test your understanding of nullable value types.

Recap

Nullable value types let value types also hold null. Write them as int?, which equals Nullable<int>.

Use HasValue to test, Value to read (only when present), and GetValueOrDefault() for a safe fallback. Reference types are already nullable on their own.

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 “Nullable Value Types” lesson free?

Yes — the full text of “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 “Nullable Value Types”?

int? and the Nullable struct. 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 “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. Nullable Value Types
  2. Null-Conditional Operator
  3. Null-Coalescing Operators
  4. Guarding Against Nulls
← Back to C# Academy