You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In C# 8.0 with the nullability analysis turned on, libraries that don't carry explicit nullable annotations are treated as "oblivious", which means the analysis assumes the best (to minimize nullability warnings).
This is generally a reasonable assumption, but it hides some potentially useful warnings. As a rule of thumb:
any API which could return a null should declare that.
any API which throws an exception on a null input should declare that. For instance, Path.GetDirectoryName(string! name).
The experience of using nullability analysis on APIs can be further enhanced with some attributes that provide hints about the semantics of the API:
In C# 8.0 with the nullability analysis turned on, libraries that don't carry explicit nullable annotations are treated as "oblivious", which means the analysis assumes the best (to minimize nullability warnings).
This is generally a reasonable assumption, but it hides some potentially useful warnings. As a rule of thumb:
null
should declare that.null
input should declare that. For instance,Path.GetDirectoryName(string! name)
.The experience of using nullability analysis on APIs can be further enhanced with some attributes that provide hints about the semantics of the API:
bool string.IsNullOrEmpty([NotNullWhenFalse] string value)
bool string.IsNullOrWhiteSpace([NotNullWhenFalse] string value)
[MaybeNull] T FirstOrDefault(...)
Debug.Assert([EnsuresTrue] ...)
We are still working to finalize the set of such attributes (tracked by dotnet/roslyn#26761).
I'm opening this issue to collect such examples, so that we have some idea which APIs should be annotated first, once we are ready to do that.
The text was updated successfully, but these errors were encountered: