Why does Visual Studio Type a Newly Minted Array as Nullable?

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 = ""; the var is inferred as string?.

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.

Leave a Comment