Will using 'var' affect performance?

后端 未结 12 1603
南方客
南方客 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:31

    If the compiler can do automatic type inferencing, then there wont be any issue with performance. Both of these will generate same code

    var    x = new ClassA();
    ClassA x = new ClassA();
    

    however, if you are constructing the type dynamically (LINQ ...) then var is your only question and there is other mechanism to compare to in order to say what is the penalty.

提交回复
热议问题