Will using 'var' affect performance?

后端 未结 12 1662
南方客
南方客 2020-11-22 14:01

Earlier I asked a question about why I see so many examples use the varkeyword and got the answer that while it is only necessary for anonymous types, that it is used noneth

12条回答
  •  甜味超标
    2020-11-22 14:26

    I don't think you properly understood what you read. If it gets compiled to the correct type, then there is no difference. When I do this:

    var i = 42;
    

    The compiler knows it's an int, and generate code as if I had written

    int i = 42;
    

    As the post you linked to says, it gets compiled to the same type. It's not a runtime check or anything else requiring extra code. The compiler just figures out what the type must be, and uses that.

提交回复
热议问题