views:

2208

answers:

7

Earlier I asked a question about why I see so many examples use the 'var' keyword and got the answer that while it's only necessary for anonymous types, that it is used nonetheless to make writing code 'quicker'/easier and 'just because'.

Following this link I saw that var gets compiled down to the correct type in the IL (you'll see it about midway down article).

My question is how much more, if any, IL code does using the 'var' keyword take, and would it be even close to having a measurable level on the performance of the code if it was used everywhere?

Thanks, Jeff

edit: Previous question is here.

+47  A: 

There's no extra IL code for the var keyword: the resulting IL should be identical for non-anonymous types. If the compiler can't create that IL because it can't figure out what type you intended to use, you'll get a compiler error.

Joel Coehoorn
Good to know, thank you.
Jeff Keslinke
Not only should the IL be identical - it *is* identical. var i = 42; compiles to exactly the same code as int i = 42;
Brian Rasmussen
Note that I used var in the IDE after compile and use reflector to extract the code. We get this. string fileName = Console.ReadLine(); string input = Console.ReadLine();
Jonathan Shepherd
+3  A: 

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

Michael Burr
+5  A: 

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.

kronoz
+1  A: 

if the compiler can do automatic type inferencing, then there wont be any issue with performance. Both of these will generate same code

var x = new ClassA(); ClassA x = new ClassA();

however, if ur constructing the type dynamically (LINQ ...) then 'var' is ur only question and there is other mechanism to compare to in order to say what is the penalty.

+3  A: 

I don't think you properly understood what you read. If it gets compiled to the correct type, then there is no difference. When I do this:

var i = 42;

The compiler knows it's an int, and generate code as if I had written

int i = 42;

As the post you linked to says, it gets compiled to the same type. It's not a runtime check or anything else requiring extra code. The compiler just figures out what the type must be, and uses that.

jalf
A: 

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.

Brian Rudolph
the RHS has to have its type calculated anyways -- the compiler would catch mismatched types and throw an error, so not really a cost there, I think.
Jimmy
+1  A: 

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!

RichardOD