0

I'm learning c# and was wondering if it is considered bad practice to use var in c#. Isn't it better to just specify the type beforehand?

Thanks.

  • No, it is not bad practice. This is only esthetic feature. If you prefer to see full type names, don't use it. – FCin May 13 '18 at 19:28
  • 1
    Personally, I like [this answer](https://stackoverflow.com/a/20392024/4934172). – 41686d6564 stands w. Palestine May 13 '18 at 19:29
  • Thank you @AhmedAbdelhameed that answer really cleared things up for me. – Ivo van der Bruggen May 13 '18 at 19:32
  • Read [the duplicate](https://stackoverflow.com/questions/1873873/why-does-resharper-want-to-use-var-for-everything) (specially the comments [here](https://stackoverflow.com/a/1873909/2141621)) rather than this duplicate. – Camilo Terevinto May 13 '18 at 19:40
  • If you look at a single line of code (an expression to be exact) and you can infer the type then use `var`. Otherwise use the type. This is the convention I follow. `var l = new List();` It is obvious the type is `List`. However, `var l = Foo();` is not obvious so it forces the reader to either look at the signature of `Foo()` to see the type it returns or scroll over the method call and read the tooltip in the IDE. In other words, be nice to yourself amd other devs and make it easy so they can read and make sense of the code without jumping around. – CodingYoshi May 13 '18 at 19:42

0 Answers0