How to initialize var?

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

Can I initialize var with null or some empty value?

11条回答
  •  醉梦人生
    2020-11-27 16:08

    you cannot assign null to a var type.

    If you assign null the compiler cannot find the variable you wanted in var place.

    throws error: Cannot assign to an implicitly-typed local variable

    you can try this:

    var dummy =(string)null;
    

    Here compiler can find the type you want so no problem

    You can assign some empty values.

    var dummy = string.Empty;
    

    or

    var dummy = 0;
    

提交回复
热议问题