Does var keyword in C# cause boxing?

后端 未结 6 1024
余生分开走
余生分开走 2020-12-10 10:05

My boss forbids me to use var as it would cause boxing and slowing down the app.

Is that true?

6条回答
  •  感动是毒
    2020-12-10 10:49

    Following on from Aakash's answer, here is the IL: (thanks LINQPad)

    WithInt:
    IL_0000:  ldc.i4.5    
    IL_0001:  stloc.0     
    IL_0002:  ldloc.0     
    IL_0003:  call        System.Console.WriteLine
    IL_0008:  ret         
    
    WithVar:
    IL_0000:  ldc.i4.5    
    IL_0001:  stloc.0     
    IL_0002:  ldloc.0     
    IL_0003:  call        System.Console.WriteLine
    IL_0008:  ret      
    

提交回复
热议问题