0

Is there any benefit from declaring the type of var to be assigned

List<myType> example1 = methodToGetMyTypeList();

over using var for the same efect

var example2 = methodToGetMyTypeList();

besides the fact that if by some mistake i'm assigning the wrong type i'll get an error and it's best suited for readability and maintenance? In terms of performance.

FabioG
  • 2,936
  • 3
  • 31
  • 50
  • 3
    declaring the type makes the code better to read / maintain – fubo Nov 12 '15 at 10:57
  • There's no functional benefit - the two lines compile to the exact same thing. There is a potential (debatable and highly debated) readability benefit. For the record I prefer using var wherever possible - I find it both easier to digest (declarations are less intrusive when reading) and more efficient to produce. – Ant P Nov 12 '15 at 10:58
  • 2
    @Ciara var is still statically typed. It makes absolutely no difference in that regard. – Ant P Nov 12 '15 at 11:01
  • 4
    This is one of my biggest bug-bears with C# developers. IMO "var" should only be used to declare anonymous types, because it's the ONLY way to declare anonymous types. Everything else should be declared explicitly because it improved code readability and maintenance. I don't give a damn if you can infer the type by using intellisense, because when I read your code on GitHub or open it using Notepad, I don't know what is going on because you've been lazy and used var. – Matthew Layton Nov 12 '15 at 11:02
  • From a factual perspective, there is no performance benefit; "var" is statically typed, inferred by the compiler and explicitly typed at compile time (try ILSpy'ing an assembly using var and see what happens). There is only benefit to code readability (and that's more of an opinion) – Matthew Layton Nov 12 '15 at 11:04
  • Ok I get it that using var can help readability and maintenance specially when working on a team. The main focus of my question was if there is any performance benefits either way. – FabioG Nov 12 '15 at 11:06
  • @series0ne what is more readable: `List strings = new List()` or `var strings = new List()`? There are situations apart from anonymous types where it is useful – thumbmunkeys Nov 12 '15 at 11:06
  • @Ant P So if I understand you correctly passing around a var doesn't run the risk of e.g. sending an incorrect type into a method because the compiler will pick this up? – Ciara Nov 12 '15 at 11:07
  • @Ciara yes, there is no risk, the compiler is able to infer the static type – thumbmunkeys Nov 12 '15 at 11:09
  • 1
    @Ciara - correct. `var` just tells the compiler to infer the type, which it will always be able to do based on the assigned value. You'll still get compilation errors if you do something like declare `var x = "Foo"` and then try to e.g. add `x` to a List of ints. – Ant P Nov 12 '15 at 11:09
  • @thumbmunkeys `List strings = new List();` I would ALWAYS do it that way for the sake of consistency, and I would argue that there are situations apart from anonymous types where it is lazy! – Matthew Layton Nov 12 '15 at 11:10
  • 2
    @series0ne different strokes for different folks, I guess :) – thumbmunkeys Nov 12 '15 at 11:18

0 Answers0