I would like to extend an existing answer by adding some links
C# specification proposal says:
nullable implicitly typed local variables
var
infers an annotated type for reference types. For instance, in
var s = ""
; thevar
is inferred asstring?
.
It means that var
for reference types infers a nullable reference type. This works if nullable context
is enabled either using project file or #nullable
pragma.
This behavior was discussed in this LDM and implemented in this issue.
This is a reason for making var
infer a nullable reference type:
At this point we’ve seen a large amount of code that requires people
spell out the type instead of using var, because code may assign null
later.