23

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.

Community
  • 1
  • 1
user9969
  • 15,632
  • 39
  • 107
  • 175
  • 1
    "In this job they hate third party tools", I used to be one of those, for sake of sanity and doing more important things with my time and making code management easier/cleaner and consistent can't not use Resharper or any 3rd party tools that will give the advantage. Just hating to use something is not a reason to stop someone else from using it. – jimjim Nov 30 '16 at 22:07

2 Answers2

54

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • thanks a lot for very detailed answer.Really appreciate it.I dont use everywhere but just makes life easier and as you pointed out there are no differences performance wise then why not.Thanks again for your time – user9969 Oct 27 '10 at 06:43
  • So would it be correct to say there's a very small _compilation_ performance benefit for using explicitly typed variables vs using var? (Makes sense that runtime performance would be identical.) – paz Jul 19 '13 at 15:33
  • @Edgar: I wouldn't like to say about compilation performance benefits. If they exist at all, I doubt that they're measurable for reasonable code, and I wouldn't want to predict which way the benefit would go without any evidence. – Jon Skeet Jul 19 '13 at 15:35
  • I think that var is better in terms of resource usage. It saves hard disk space and key strokes. since c# is used in all over world usage of var can save energy. But we need to decide based on comparing over all resource utilisation. So if some one is concerned saving energy they need to make var as choice. But if the type has to be visible to the developer when reading the code and avoid one extra step in the compilation then they can choose explicit – last-Programmer Jun 20 '19 at 16:29
  • 1
    @rbmanian75: That would suggest using single-character variable names as well. When talking about "resource usage", the size of source code on disk is almost never an important factor. Efficiency is about execution speed etc, not how much space something takes on disk for the source code. – Jon Skeet Jun 20 '19 at 16:32
  • @JonSkeet variable names are decided by who codes. But when declaring type according to the grammar of programming language has few choices. Hope you get what it says. Just give a thought if you are living in the past the disk space also an important factor. Think about millions of programmers if choose to use var how much space it can save. It is not about one person it is about the world – last-Programmer Jun 20 '19 at 20:42
  • 1
    @rbmanian75: "variable names are decided by who codes" - as is the decision of whether to use `var` or not. I'm not living in the past though. I'm living in a world where a *single image stored on disk* is likely to have the same impact as changing to use implicitly typed variables for hundreds of thousands of declarations. If those hundreds of thousands of use of `var` for the sake of shaving off disk space lead to even a single bug that needs investigating, that's likely to require more resources than everything saved. Forget disk space: code for readability. – Jon Skeet Jun 21 '19 at 06:15
  • @rbmanian75: Note that you can code in C# with hardly any whitespace, too. You don't need new lines, for example. Is all of your code on a single line, with no unnecessary spacing? I assume not. If not, do you use any indentation? Is it single space indentation? Doing *that* would save far more disk space than using `var` everywhere - but it hurts readability, so I reject it. – Jon Skeet Jun 21 '19 at 06:16
  • @Jon the problem here is not the white space at present. – last-Programmer Jun 21 '19 at 10:13
  • @rbmanian75: I don't understand your argument then. I thought you were arguing that the space of source code on disk is important - in which case removing all unnecessary white space would save a lot more space than using `var` instead of explicit declarations. Surely you should be in favour of that too, if you're seriously making the argument that our coding practices should be dictated by space on disk. (And we should make all identifiers single characters as well - why use a long, descriptive name when you can use a single letter one?) I just don't buy it as an important factor. – Jon Skeet Jun 21 '19 at 10:15
11

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

anishMarokey
  • 11,279
  • 2
  • 34
  • 47