Interview frequency: …
Short answer
They distinguish references that allow null (such as string?) from those that do not (string); the compiler uses static analysis to warn before a NullReferenceException occurs at runtime.
Explanation
Since C# 8, developers can enable <Nullable>enable</Nullable>. In this mode, the compiler assumes object references are non-null unless explicitly marked with ?. It performs Static Flow Analysis: after checking if (user != null), the compiler knows the variable is safe inside the block and allows property access without warnings.
Code example
string nonNull = "Hello"; // Does not accept null
string? canBeNull = null; // Allowed
// Console.WriteLine(canBeNull.Length); // Compiler warning!
if (canBeNull != null) {
Console.WriteLine(canBeNull.Length); // Safe after flow analysis
}Common mistakes
- Using the Null-forgiving operator (!) such as user!.Name to bypass a compiler warning without a real check, causing a runtime crash.