C# Nullable Types
Declare int? size. Assign 4. Write size.HasValue and then write size.Value. Keep the work in Pine with Main. You do not need System.Collections.Generic for this page's kept file. Run with dotnet run. The screen should show two lines: True then 4. An int? can hold a number or null. HasValue says whether a number is present. Value reads that number when it is present. Keep the starting public class named Pine.
A value type that can also mean empty
On the null page earlier, a string could hold null, and a plain int could not. This page adds the nullable form int?. The question mark on the type means the name may hold an int or may hold null. You still use ordinary int when a number is always required. You use int? when "no number yet" is a real state you need to store. Think of the box as either empty or holding one number — never both at once.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any leftover foreach over bag that only prints list values. Delete any HashSet Count leftover. Delete any ReadLine line. The file you keep has no wait for typing. A clean Main makes the two-line nullable screen easy to trust. Mixing a foreach leftover into Main can print 3 then 8 instead of the nullable lines.
At the top keep using System;. Inside Main, for a first check, write int? size = null; then Console.WriteLine(size.HasValue);. Save and run that short shape. The screen shows False because no number is stored. Then extend to int? size = 4; with both HasValue and Value writes as the kept demo. The null-HasValue run is only a checkpoint; the page's kept answer is still True then 4.
Do not write int size = null; without the ? on the type. That build fails. The ? is part of the type name you type: int?. Leave a space out of int ? as a habit — write int? with the mark against the word int.
using System;
public class Pine
{
public static void Main()
{
int? size = null;
Console.WriteLine(size.HasValue);
}
}
Pine stores 4 and writes HasValue then Value
Inside Main, write int? size = 4; or assign size = 4; after a null start. Write size.HasValue first so the screen shows that a number is present. Then write size.Value so the screen shows the number itself. You are not walking a list with foreach on the kept file. The kept demo is the nullable int.
using System;
public class Pine
{
public static void Main()
{
int? size = 4;
Console.WriteLine(size.HasValue);
Console.WriteLine(size.Value);
}
}
Read the kept file from the top. using System; prepares console names. public class Pine is the starter. public static void Main() is where the runtime starts. int? size = 4; stores a number in a nullable int. Writing size.HasValue prints True. Writing size.Value prints 4.
Run it with dotnet run. The program writes two lines and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep the nullable work. The screen shows True then 4. If the screen shows 3 then 8, a leftover foreach list demo may still be running. If the screen shows only False, you are on the null-HasValue checkpoint — switch to the file that assigns 4 before the two writes.
If you read size.Value while size is still null, the run can throw. Check HasValue or compare to null before you read Value when the name might be empty. On the kept demo, 4 is already stored, so Value is safe. If you write int size = null; without ?, the build fails — put the ? back on the type.
int versus int? on this page
A plain int always holds a number. An int? may hold a number or null. HasValue is False when the nullable name is null, and True when a number is stored. Value is the number itself when HasValue is True.
A leftover foreach (int n in bag) demo is a different lesson. For this page keep int? size and the HasValue/Value writes. Mixing both demos in one Main can print list numbers instead of the nullable lines — remove the foreach work while you practice int?.
📊 int vs int?
| Form | Meaning on this page |
|---|---|
int size = 4; |
always a number; cannot be null |
int? size = null; |
no number stored yet; HasValue is False |
int? size = 4; |
number present; HasValue is True; Value is 4 |
What this leaves for exceptions
This page declares int?, stores null or a number, reads HasValue, and reads Value. A later page — exceptions — covers errors you catch with try and catch. Stay with nullable value types here. Do not invent exception syntax on this page; the lesson goal is int? itself. When dotnet run prints True then 4 from the kept file, you have the checkpoint this page asks for.
Do not nest a second Main. Do not leave foreach bag demos as the only teaching path. If the screen shows only False, use the file that assigns 4. If the build rejects null on an int, you forgot the ? on the type. Keep int? spelled with the question mark next to int.
You can write int? size = 4;, then write size.HasValue and size.Value. Run with dotnet run. The screen shows True then 4. When size was null, HasValue was False on the earlier check. Think of int? as a box that may hold a number or may be empty. HasValue asks whether the box has a number. Value reads the number when the box is not empty. Do not read Value while the box is empty on purpose — that path can throw. Pine stays the door you open with dotnet run. The next page is exceptions, where you catch errors instead of letting the program stop hard.
Keep the ? on int?. Keep leftover foreach demos out of Main so the screen cannot show 3 then 8 by mistake. Those habits are the checkpoint before you move on. You still use plain int when a missing number is not allowed. You use int? when empty is a state you must represent. The two forms sit side by side in real programs; this page only asks you to prove the nullable form with True then 4.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top