How to initialize var?

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

Can I initialize var with null or some empty value?

相关标签:
11条回答
  • 2020-11-27 15:59

    Why wouldn't this be possible?

    var theNameOfTheVar = (TheType)null;
    

    eg:

    var name = (string)null;
    

    Simple as that.

    0 讨论(0)
  • 2020-11-27 16:00

    Nope. var needs to be initialized to a type, it can't be null.

    0 讨论(0)
  • 2020-11-27 16:02

    Thank you Mr.Snake, Found this helpfull for another trick i was looking for :) (Not enough rep to comment)

    Shorthand assignment of nullable types. Like this:

    var someDate = !Convert.IsDBNull(dataRow["SomeDate"])
                        ? Convert.ToDateTime(dataRow["SomeDate"])
                        : (DateTime?) null;
    
    0 讨论(0)
  • 2020-11-27 16:03

    This is the way how to intialize value to var variable

    var _myVal = (dynamic)null; 
    
    0 讨论(0)
  • 2020-11-27 16:04

    Well, as others have stated, ambiguity in type is the issue. So the answer is no, C# doesn't let that happen because it's a strongly typed language, and it deals only with compile time known types. The compiler could have been designed to infer it as of type object, but the designers chose to avoid the extra complexity (in C# null has no type).

    One alternative is

    var foo = new { }; //anonymous type
    

    Again note that you're initializing to a compile time known type, and at the end its not null, but anonymous object. It's only a few lines shorter than new object(). You can only reassign the anonymous type to foo in this one case, which may or may not be desirable.

    Initializing to null with type not being known is out of question.

    Unless you're using dynamic.

    dynamic foo = null;
    //or
    var foo = (dynamic)null; //overkill
    

    Of course it is pretty useless, unless you want to reassign values to foo variable. You lose intellisense support as well in Visual Studio.

    Lastly, as others have answered, you can have a specific type declared by casting;

    var foo = (T)null;
    

    So your options are:

    //initializes to non-null; I like it; cant be reassigned a value of any type
    var foo = new { }; 
    
    //initializes to non-null; can be reassigned a value of any type
    var foo = new object();
    
    //initializes to null; dangerous and finds least use; can be reassigned a value of any type
    dynamic foo = null;
    var foo = (dynamic)null;
    
    //initializes to null; more conventional; can be reassigned a value of any type
    object foo = null;
    
    //initializes to null; cannot be reassigned a value of any type
    var foo = (T)null;
    
    0 讨论(0)
  • 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 <null> 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;
    
    0 讨论(0)
提交回复
热议问题