C# Academy · Lesson

HasValue and Value Properties

Inspect and access nullable values safely.

Lesson 2 of 413 steps

HasValue and Value Properties is a free C# Academy lesson on CoddyKit. This is lesson 2 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.

Two Key Properties

Every nullable exposes HasValue (a bool) and Value (the underlying value). They are your tools for inspecting it safely.

using System;

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

HasValue Checks Presence

HasValue is true when a real value is present and false when it is null.

using System;

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

Reading Value Safely

Always check HasValue before reading Value, because reading Value on a null throws.

using System;

class Program {
    static void Main() {
        int? n = 42;
        if (n.HasValue) {
            Console.WriteLine(n.Value * 2);
        }
    }
}

Accessing Value on Null Throws

If you read Value when the nullable is null, C# throws an InvalidOperationException. The check protects you.

using System;

class Program {
    static void Main() {
        int? n = null;
        try {
            Console.WriteLine(n.Value);
        } catch (InvalidOperationException) {
            Console.WriteLine("Caught: no value");
        }
    }
}

GetValueOrDefault

GetValueOrDefault() returns the value if present, otherwise the type default (0 for int, false for bool).

using System;

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

GetValueOrDefault with Fallback

Pass an argument to choose your own fallback: GetValueOrDefault(99) returns 99 when null.

using System;

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

Pattern: Check Then Use

A common pattern combines HasValue and Value into a clean branch.

using System;

class Program {
    static void Describe(int? score) {
        if (score.HasValue)
            Console.WriteLine("Score is " + score.Value);
        else
            Console.WriteLine("No score yet");
    }
    static void Main() {
        Describe(88);
        Describe(null);
    }
}

Implicit Conversion Back

You can assign a nullable to a plain type only after confirming it has a value, then read Value.

using System;

class Program {
    static void Main() {
        int? maybe = 15;
        if (maybe.HasValue) {
            int real = maybe.Value;
            Console.WriteLine(real + 5);
        }
    }
}

Default of Nullable

A nullable not explicitly set defaults to null, so HasValue is false.

using System;

class Program {
    static void Main() {
        int? n = default;
        Console.WriteLine(n.HasValue);
    }
}

Using in Aggregations

Loop over data, skipping nulls with HasValue, to compute safe totals.

using System;

class Program {
    static void Main() {
        int?[] readings = { 10, null, 5, null, 8 };
        int sum = 0;
        foreach (var r in readings)
            if (r.HasValue) sum += r.Value;
        Console.WriteLine(sum);
    }
}

GetValueOrDefault in Loops

GetValueOrDefault can simplify code where null should be treated as zero.

using System;

class Program {
    static void Main() {
        int?[] readings = { 10, null, 5 };
        int sum = 0;
        foreach (var r in readings)
            sum += r.GetValueOrDefault();
        Console.WriteLine(sum);
    }
}

Quick Check

Test your knowledge of nullable properties.

Recap

HasValue tells you whether a nullable holds a value, and Value retrieves it, but reading Value on a null throws.

GetValueOrDefault() safely returns the value or a fallback (the type default or one you supply), making it ideal for sums and safe reads.

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 “HasValue and Value Properties” lesson free?

Yes — the full text of “HasValue and Value Properties” 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 “HasValue and Value Properties”?

Inspect and access nullable values safely. 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 2 of 4.

How long does the “HasValue and Value Properties” 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