Will using 'var' affect performance?

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

    As Joel says, the compiler works out at compile-time what type var should be, effectively it's just a trick the compiler performs to save keystrokes, so for example

    var s = "hi";
    

    gets replaced by

    string s = "hi";
    

    by the compiler before any IL is generated. The Generated IL will be exactly the same as if you'd typed string.

提交回复
热议问题