20

Possible Duplicate:
C# 'var' vs specific type performance

Hi all,
I recently saw code that uses var a lot.
E.g.:

var myString = GetAnyString();
var myInstance = GetClass();

instead of

string myString = GetAnyString();
MyClass myInstance = GetClass();

Besides the readability (I think that using var is not very readable), are there any advantages and/or drawbacks using it? How about performance

Community
  • 1
  • 1
Simon Linder
  • 3,378
  • 4
  • 32
  • 49
  • "C# 'var' vs specific type performance" question does not cover question being asked. – Andrew Bezzub Oct 29 '10 at 09:26
  • There are many discussions about `var` on the site on various aspects of it. From the performance (which was chosen), the practicality of its use, the subjective nature as to why its used in the first place, etc. All still relevant today. Take your pick, it's a duplicate. – Jeff Mercado Oct 29 '10 at 09:34
  • 2
    "Besides the readability (I think that using var is not very readable)" -- no, var is more readable than redundant mentions of the type, which is one of the reasons that modern languages have type inference. "How about performance" -- this suggests a severe misunderstanding of the basics of the language ... C# is statically typed. – Jim Balter Aug 27 '14 at 00:38
  • 1
    I use `var` whenever possible, less typing is all. Also Visual Studio or any decent IDE will show the correct type when hovering over variable's name. – Vahid Amiri Aug 05 '16 at 11:10

3 Answers3

22

It is subjective, but I don't like using vars. As for me they don't improve readability.

But general guideline on var usage is following:

Use var when it is absolutely clear what type of the variable is being declared:

Dictionary<string, Dictionary<string, string>> varOfSomeLongType = new Dictionary<string, Dictionary<string, string>>();

or when you are declaring variable of anonymous type:

var linqResult = from element in SomeCollection
                 select new { element.A, element.B }
Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
  • 8
    LINQ is one of the few situations where to use var in my opinion. – Simon Linder Oct 29 '10 at 09:31
  • I think it's more that @Andrew Bezzub was using LINQ to make an anonymous type. A lot of my experience (which probably differs from a lot of other people's), I usually need the results of the LINQ query immediately, and/or the types LINQ spits back are still pretty readable. If it starts getting too ugly, I usually try to reassess how I'm going at it, since it's probably getting dangerously difficult to maintain at that point, anyway. If I use var, that's a lot harder to spot and fix. – Gurgadurgen Dec 16 '15 at 17:54
13

var is replaced by the compiler with the right type, so there is no runtime performance hit, aside from a very small compile time overhead maybe (veeeeeeeery small).

Actually, from a readability point of vue, I personally do as follows:

  • Variable instantiation: object myVar = new object();

  • Variable propagation: var myVar = anotherVar.getComplexObject();

Also, you might be tempted to use them when you have a very complex type instantiation, like:

var myDict = new Dictionary<string, List<Tuple<string, object>>>();

Here, it really improves readability.

Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54
  • 2
    Actually, currently working on a Java project, I find myself being VERY frustrated that the var keyword is unavailable is Java, especially during variable propagation, when you DON'T want to search the documentation in order to know what type is that variable you got and how to write it correctly. – Aurelien Ribon Oct 29 '10 at 09:39
  • 16
    How can you know how to use the variable if you don't know it's type? – Goldorak84 Dec 09 '11 at 15:50
  • 1
    Just as a curiosity: in Java 7+, the above `Dictionary` example can be written as `Map>> myMap = new HashMap<>();`, using the new "diamond operator" (well, except that there is no `Tuple` type in the JDK). – Rogério Mar 01 '12 at 12:14
  • 2
    @Goldorak84 I know this is old, but using `var` + IntelliSense is a quick way to find out what type it is. Just use `var`, then put your mouse over the variable, and boom! it tells you what type it is. Super handy when using external libraries or writing complicated Linq queries. – Gabriel Luci Nov 10 '18 at 04:41
4

are there any advantages and/or drawbacks using it?

An advantage: less typing.

(I think that using var is not very readable)

I partially agree with this point.

It is less readable when you have var s = foo(); instead of string s = foo;. In other cases it can be more readable.

How about performance

At runtime there is no difference. The compiler converts it into the same intermediate code as if you had explicitly stated the type.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 2
    "It is less readable when you have var s = foo(); instead of string s = foo;" -- the readability problems come entirely from having meaningless names. Knowing that a thing with a meaningless name obtained from a method with a meaningless name is a string doesn't help. I now use var/auto as an interview filter ... people who think that type inference makes code less readable don't get hired. – Jim Balter Aug 27 '14 at 00:45
  • 3
    Jim Balter are you saying that the variable name should infer the type? Because that sounds like undoing your "var" to me ie more typing – Epirocks Mar 23 '17 at 11:26