Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

var vs explicit declaration [duplicate]

Tags:

Possible Duplicate:
Use of var keyword in C#

Hi, Just moved job and I am used to using var a lot. At my previous job we were doing lots of TDD and using resharper.

In this job they hate third party tools and the developers here say that it is not good to use var all the time and it is not as efficient as explicit typing.

Some time ago I thought the same but now I have gotten so used to it, and it makes my code look neater.

I have read some posts and there seems to be confusion whether it is as efficient or not. I read that using var produces the same IL code. So should it not be as efficient? Somewhere else I read that even though using var produces the same IL code it has to find out what type it is. So what does 'inferred' really mean then?

Some clarification as to whether performance wise they are the same would be fantastic.

like image 643
user9969 Avatar asked Oct 27 '10 06:10

user9969


People also ask

Why is var better than type C#?

var requires less typing. It also is shorter and easier to read, for instance, than Dictionary<int,IList> . var requires less code changes if the return type of a method call changes. You only have to change the method declaration, not every place it's used.

What is the advantage of var keyword?

It means compiler will set the correct data type based upon the data to be assigned. The actual purpose of var keyword is to hold something which is anonymous type and we see anonymous type frequently in LINQ or while creating anonymous type object.

Does var affect performance C#?

Performance impact of using VAR in C#No, because at compile type it results in the same code as if you had explicitly typed your code. Using the dynamic keyword does have some performance implications though.

What does var do in JavaScript?

var is the keyword that tells JavaScript you're declaring a variable. x is the name of that variable.


2 Answers

In terms of execution time efficiency, it makes no difference. It is indeed compiled into exactly the same IL. The variable will still be statically typed, it's just that the type is inferred from the initialization expression.

"Inferred" means "worked out from other information". So if the declaration is:

string x = "hello"; 

the variable x is explicitly declared to be of type string. The compiler doesn't have to work anything out. If you use var:

var x = "hello"; 

then the compiler finds the compile-time type of the expression being assigned, and makes that the type of the variable. x is still known to be a string variable everywhere else in the code.

If the developers you're working with think that var behaves dynamically, I would be very cautious about other information they tell you. You might want to discreetly suggest that they learn a bit more about how new language features work before judging them.

In terms of developer efficiency, that's a much harder question to judge. Personally I use var quite a bit - particularly in tests - but not everywhere.

like image 174
Jon Skeet Avatar answered Oct 08 '22 20:10

Jon Skeet


var is converted to is exact type during compilation.no problem in efficiency as per my knowledge.

IL for this code is shown below:

class Foo     {         void Bar()         {             var iCount = 0;             int iCoount1 = 0;         }     } 

alt text

like image 30
anishMarokey Avatar answered Oct 08 '22 19:10

anishMarokey