However, the use of var does have at
least the potential to make your code
more difficult to understand for other
developers. For that reason, the C#
documentation generally uses var only
when it is required.
I really, really don't like implicit typing. On the surface it tends to make code more readable, but can lead to lots of problems down the road. If a dev changes a variable initializer, say, from
The type will change, resulting in whole slew of compiler errors or, if it's in a web view and you're not using the post-build step to precompile your views, a whole slew of runtime errors that won't be caught without effective pre-deployment testing.
Implicit typing also doesn't work everywhere (from the same MSDN link)
var can only be used when a local
variable is declared and initialized
in the same statement; the variable
cannot be initialized to null, or to a
method group or an anonymous function.
var cannot be used on fields at class
Variables declared by using var cannot
be used in the initialization
expression. In other words, this
expression is legal: int i = (i = 20);
but this expression produces a
compile-time error: var i = (i = 20);
Multiple implicitly-typed variables
cannot be initialized in the same
If a type named var is in scope, then
the var keyword will resolve to that
type name and will not be treated as
part of an implicitly typed local
Keeping your code consistent (in this case, using explicit typing everywhere) is a very, very good thing. In my opinion,
var is lazy and provides no real benefit, and introduces yet another potential point of failure in an already complex process.
I completely changed my mind. When working in C#, I use
var most of the time (excepting things like interface-type variables and such). It keeps the code terse which improves readability. Still, though - pay attention to what the resolved type really is.