Member-only story
The Question Mark Is a Maybe
What string?, int?, null, ?., ?? and ! actually mean in C#
Learning C# means learning its syntax. You get used to parentheses, braces and semicolons fairly quickly. Then characters such as ? start appearing in places where their purpose is much less obvious.
So today, let’s focus on just one of them. What does ? mean in C#?
The slightly inconvenient answer is that it depends on where you find it. You might see it attached to a type
string? name = null;
int? age = null;You might find it before a dot
int? length = name?.Length;Or doubled
string displayName = name ?? "Unknown";And just when the question mark starts making sense, an exclamation mark appears
int length = name!.Length;These symbols are not doing exactly the same thing, but most of them revolve around the same problem.
Sometimes a value might not be there. To understand what C# does about that, we need to start with null.
When Nothing Is a Valid Value
null represents the absence of a value. That sounds simple enough, but absence can carry useful…









