Member-only story
var behavior if you have NRT (nullable reference type) enabled
You can consider it as a small note for me. Since I’m involved in writing the C# code style guidelines I recalled that it was some misunderstandings in var behavior in NRT (nullable reference type) regard.
Long story short
var list = new List<int>();
Thas is the type of var? It depends. If you don’t have nullable type enabled (<nullable>enabled</nullable> in your project file or #nullable enable in the code file) it is still List<int>, but if you have— List<int>? .
The most important thing to remember — When
var
is used with nullable reference types enabled, it always implies a nullable reference type even if the expression type isn't nullable.
Still, you do not need to worry, because the compiler correctly tracks the state of the variable, i.e, it won’t show a warning when you dereference a variable unless you assign it a nullable value later on. So var type doesn’t seem to be the problem.
TL;DR
The decision to make var is unintuitive and may come as a surprise. Some developers even state that they are not going to be able to sleep until the code rules force me to avoid var on this case (in case the actual type is NRT).
The main reasons are best practices (the design and goal of the language here are to make it find to have nullable types as flow analysis ensures you don’t have null-values used in a way that would…