How to initialize var?

前端 未结 11 1910
庸人自扰
庸人自扰 2020-11-27 15:29

Can I initialize var with null or some empty value?

11条回答
  •  孤独总比滥情好
    2020-11-27 16:15

    var just tells the compiler to infer the type you wanted at compile time...it cannot infer from null (though there are cases it could).

    So, no you are not allowed to do this.

    When you say "some empty value"...if you mean:

    var s = string.Empty;
    //
    var s = "";
    

    Then yes, you may do that, but not null.

提交回复
热议问题