The var keyword was introduced with C# 3.0 and the .NET Framework 3.5 to allow the declaration of implicitly typed variables. The driving force behind the need for implicitly typed variables was the introduction of Anonymous Types.
If you are using var outside of truly anonymous types you are doing so out of laziness. I know this is a bold statement that a lot of developers are going to disagree with, so let me explain.
First off I totally agree that using var can speed up your lines per minute coded and I do use var in this manner. But, I always replace var with the actual type after the fact. You can do this with out requiring a large amount of time after the fact using a tool such as ReSharper and setting up you Clean Up Code” functionality to replace all usages of var with the actual type when possible.
Now for my supporting arguments: *All of which only pertain to using var when the type is not truly anonymous.*
The truth is you know what your result is, at least you better or you have no business writing the code in the first place, so just declare it as such!
One more thing, var has no place in demo code, tutorials, and blog posts. The purpose of demo code, tutorials, and blog posts are to provide education and in doing so the code should be clean and concise and accessible to the masses.
OK, let me have it.
Keith is a Senior Software Engineer with Falafel Software. He has been developing software since 1999 specializing in web-based solutions primarily using the Microsoft stack. He has been a Microsoft MVP in ASP.NET since 2012.
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in any way.