Will using 'var' affect performance?

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

    As nobody has mentioned reflector yet...

    If you compile the following C# code:

    static void Main(string[] args)
    {
        var x = "hello";
        string y = "hello again!";
        Console.WriteLine(x);
        Console.WriteLine(y);
    }
    

    Then use reflector on it, you get:

    // Methods
    private static void Main(string[] args)
    {
        string x = "hello";
        string y = "hello again!";
        Console.WriteLine(x);
        Console.WriteLine(y);
    }
    

    So the answer is clearly no runtime performance hit!

提交回复
热议问题