Nullable Types
null means “no value”. It is useful — a customer may have no middle name — but reading a property of null crashes your program. C# gives you tools to handle null safely.
On this page · 5 min · Beginner
On this page
- In simple words
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
In simple words#
A form field can be filled or left blank. Nullable types let a variable say “no value yet” (null) — like a blank answer — and the compiler reminds you to check before using it.
- null
- Means “no value”.
- int?
- An int that may also be null.
- ?? and ?.
- Give a default, or safely skip when null.
Learning objectives#
- Make value types nullable with ?
- Use ?. , ?? and ??= to handle null
- Understand nullable reference types and compiler warnings
- Check for null with is null and is not null
Explanation#
Value types (int, decimal, bool, DateTime) normally cannot be null. Add ? to allow it: int? discount = null;. Use .HasValue and .Value, or simply compare to null.
Reference types (string, classes) can hold null. With nullable reference types (on by default in new projects), string means “never null” and string? means “may be null”. The compiler warns when you might use a null.
?. is the null-conditional operator: name?.Length returns null instead of crashing when name is null.
?? supplies a fallback: discount ?? 0. ??= assigns only if the variable is currently null.
Check with is null / is not null. They always do a real null check, even if a class overloads ==.
Syntax#
int? qty = null;
string? nickname = null;
int len = nickname?.Length ?? 0;
nickname ??= "Guest";
if (nickname is not null) { ... }
Code example: Missing data without crashes#
int? discount = null;
Console.WriteLine(discount.HasValue);
Console.WriteLine(discount ?? 0);
discount = 15;
Console.WriteLine(discount.Value);
string? middleName = null;
Console.WriteLine(middleName?.Length ?? 0);
middleName ??= "N/A";
Console.WriteLine(middleName);
string? email = FindEmail(2);
if (email is not null)
{
Console.WriteLine(email.ToUpper());
}
else
{
Console.WriteLine("No email on file");
}
static string? FindEmail(int customerId) =>
customerId == 1 ? "asha@example.com" : null;
False 0 15 0 N/A No email on file
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- int? can hold null. HasValue is False until a number is assigned.
- discount ?? 0 uses 0 because discount is null.
- middleName?.Length is null (no crash), and ?? 0 turns it into 0.
- ??= assigns "N/A" because middleName was null.
- FindEmail returns string? to warn callers that the result may be missing; the is not null check handles both cases.
Real-world use case#
A customer table has optional columns such as DateOfBirth and MiddleName. EF Core maps them to DateTime? and string?, and the API uses ?? to send sensible defaults to the front end.
Common mistakes#
Calling customer.Address.City when Address is null crashes. Use customer.Address?.City or check for null first.
discount.Value throws InvalidOperationException when discount is null. Use ?? or HasValue.
name!.Length tells the compiler “trust me”. If you are wrong, it still crashes. Fix the logic instead.
Best practices#
- Keep nullable reference types enabled (<Nullable>enable</Nullable>)
- Return T? only when “no result” is a normal outcome
- Use ?? to provide defaults at the edges of your code
- Prefer is null over == null
Remember this#
int? = int that may be missing.
?. = “only if not null”. ?? = “otherwise use this”.
string? warns you; string promises a value.
Practice exercise#
Your task: Write a method GetDisplayName(string? nickname, string fullName) that returns the nickname when it is not null or empty, otherwise the full name.
Need a hint?
Show solution
Console.WriteLine(GetDisplayName(null, "Asha Rao")); // Asha Rao
Console.WriteLine(GetDisplayName("Ash", "Asha Rao")); // Ash
static string GetDisplayName(string? nickname, string fullName) =>
string.IsNullOrEmpty(nickname) ? fullName : nickname;
Mini quiz#
What is printed?
string? s = null;
Console.WriteLine(s?.Length ?? -1);
Want more? Practise all C# MCQs · C# interview questions
My notes#
Saved in this browser.