Home About Lessons Blog On The Mic Contact Projects
Lessons BASIC · C#

C# Basics — Nullable & Null Safety

8 min read · Emre Ulutabak
1
What is null?

Think of an empty wallet. There's no money inside — but the wallet exists. Now imagine there is no wallet at all. That is exactly what null is: the absence of the object itself.

In software, if a variable doesn't point to any object yet, its value is null.

2
Why does null crash?

If you try to call a method on an object that doesn't exist, the program throws a NullReferenceException and crashes. This is one of the most common errors in software.

csharp
string isim = null;

Console.WriteLine(isim.Length); // PATLAMA: NullReferenceException
// isim null olduğu için .Length çağrılamaz
3
Nullable types

Value types (int, bool, DateTime) normally cannot be null. But sometimes you want to express that a number "hasn't been entered yet". For this you use ?.

csharp
int sayi = null;       // HATA — int null olamaz
int? sayi = null;      // DOĞRU — nullable int

string? isim = null;   // C# 8+ ile string de nullable olabilir

if (sayi.HasValue)
    Console.WriteLine(sayi.Value);
else
    Console.WriteLine("Değer girilmemiş.");
4
The ?. and ?? operators

?. (null-conditional): if the object is null, returns null instead of crashing.
?? (null-coalescing): if the left side is null, uses the default value on the right.

csharp
string? isim = null;

// ?. operatörü — null ise patlamaz, null döner
int? uzunluk = isim?.Length;  // null

// ?? operatörü — null ise varsayılanı kullan
string gosterim = isim ?? "Misafir";
Console.WriteLine(gosterim); // "Misafir"

// İkisini birlikte kullanmak
string sonuc = isim?.ToUpper() ?? "BİLİNMİYOR";
Console.WriteLine(sonuc); // "BİLİNMİYOR"
💡
Combining ?. and ?? reduces null checking to a single line.
5
Golden rules
💡
Skipping null checks is the most common crash cause. Always ask: can this be null?
💡
The ?. operator provides safe access — returns null instead of crashing.
💡
The ?? operator is used to provide a default value. Show something meaningful instead of null.
MINI QUIZ
Which is correct about null?
The int type can be null by default
The ?? operator uses the right-side value when the left side is null
The ?. operator crashes when the object is null
Null checking is unnecessary