Cannot assign null to an implicitly-typed variable

后端 未结 4 1616
傲寒
傲寒 2021-01-18 11:22

According to Visual Studio this is not ok:

var foo = null;

But this is ok:

var foo = false ? (double?)null : null;
<         


        
4条回答
  •  星月不相逢
    2021-01-18 11:32

    Assigning null to a var to a value, VS can't identify what type gonna be (double,int,bool etc). Var is commonly used when you don't know what type your value gonna be.

    Your second declaration pinpoints the type as Nullable that's why you don't get an exception/error.

提交回复
热议问题