I get asked this question or get in heated discussions about this all the time:
“I have a standards question regarding the var keyword. I’m starting to see it pop up a LOT in our code and I was wondering if there were any official Microsoft standards regarding its usage. I was under the impression it should only be used for anonymous types and query expressions. What are your thoughts?”
Here is my two cents on the matter:
Unfortunately this is becoming almost a religion thing. Yes, it was added for anonymous types but I personally use it on most of my type declarations (as long it’s obvious what type the variable is… good coding standards apply). Why? Here is why I like it:
- If you have to do a search and replace on a type, it’s MUCH easier if you use var.
- Makes for shorter code lines because you don’t have to type the type twice!
I doubt there is any performance issues either, it’s still strong typed. I don’t think people like it because they are used to the old way of declaring types and maybe the keyword var reminds them of an un-typed variant. In VB it’s Dim, so no confusion there.
What is your opinion?