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
- Introducing Nullable Value Types
- HasValue and Value Properties
- Null-Coalescing Operators
- Nullable in Calculations