Will using 'var' affect performance?

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

    There is no runtime performance cost to using var. Though, I would suspect there to be a compiling performance cost as the compiler needs to infer the type, though this will most likely be negligable.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-22 14:41

    "var" is one of those things that people either love or hate (like regions). Though, unlike regions, var is absolutely necessary when creating anonymous classes.

    To me, var makes sense when you are newing up an object directly like:

    var dict = new Dictionary<string, string>();
    

    That being said, you can easily just do:

    Dictionary<string, string> dict = new and intellisense will fill in the rest for you here.

    If you only want to work with a specific interface, then you can't use var unless the method you are calling returns the interface directly.

    Resharper seems to be on the side of using "var" all over, which may push more people to do it that way. But I kind of agree that it is harder to read if you are calling a method and it isn't obvious what is being returned by the name.

    var itself doesn't slow things down any, but there is one caveat to this that not to many people think about. If you do var result = SomeMethod(); then the code after that is expecting some sort of result back where you'd call various methods or properties or whatever. If SomeMethod() changed its definition to some other type but it still met the contract the other code was expecting, you just created a really nasty bug (if no unit/integration tests, of course).

    0 讨论(0)
  • 2020-11-22 14:44

    For the following method:

       private static void StringVsVarILOutput()
        {
            var string1 = new String(new char[9]);
    
            string string2 = new String(new char[9]);
        }
    

    The IL Output is this:

            {
              .method private hidebysig static void  StringVsVarILOutput() cil managed
              // Code size       28 (0x1c)
              .maxstack  2
              .locals init ([0] string string1,
                       [1] string string2)
              IL_0000:  nop
              IL_0001:  ldc.i4.s   9
              IL_0003:  newarr     [mscorlib]System.Char
              IL_0008:  newobj     instance void [mscorlib]System.String::.ctor(char[])
              IL_000d:  stloc.0
              IL_000e:  ldc.i4.s   9
              IL_0010:  newarr     [mscorlib]System.Char
              IL_0015:  newobj     instance void [mscorlib]System.String::.ctor(char[])
              IL_001a:  stloc.1
              IL_001b:  ret
            } // end of method Program::StringVsVarILOutput
    
    0 讨论(0)
  • 2020-11-22 14:44

    So, to be clear, it's a lazy coding style. I prefer native types, given the choice; I'll take that extra bit of "noise" to ensure I'm writing and reading exactly what I think I am at code/debug time. * shrug *

    0 讨论(0)
  • 2020-11-22 14:49

    The C# compiler infers the true type of the var variable at compile time. There's no difference in the generated IL.

    0 讨论(0)
提交回复
热议问题