What are nullable attributes like NotNullWhen and MaybeNull for?
Short answer
They express nullability that depends on outcomes the type alone can't show, improving flow analysis for helpers like TryGet.
Advertisement
AModel answer
They express nullability that depends on outcomes the type alone can't show, improving flow analysis for helpers like TryGet.
bool TryGet(string key, [NotNullWhen(true)] out User? user);Real-world example
After TryGet returns true, the compiler knows user is non-null, so no warning on use.