Does var keyword in C# cause boxing?

后端 未结 6 1014
余生分开走
余生分开走 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:54

    An approach that might work is to write these two methods:

    public static void WithInt()
    {
        int x = 5;
        Console.WriteLine(x);
    }
    
    public static void WithVar()
    {
        var x = 5;
        Console.WriteLine(x);
    }
    

    Compile, and use ildasm to examine the produced CIL. Show your boss.

    edit @ck has done all but the last step for you :)

提交回复
热议问题