0Pricing
C# Academy · Lesson

HasValue and Value Properties

Inspect and access nullable values safely.

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

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