I was wondering when should you use var?
Almost anything in C#, except for maybe the primitives and a few more odd cases, derive from Object.
So wouldn’t it be a better practice to use that actual types ? or at least object ?
(I’ve been programming for a while, and my strict point of view is that var is evil)
You misunderstand:
varisn’t a type.varinstructs the compiler to use the correct type for a variable, based on its initialisation.Example:
Your variables are still strongly typed when using
var.As a consequence,
varisn’t “evil”. On the contrary, it’s very handy and as I’ve said elsewhere, I use it extensively. Eric Lippert, one of the main people behind C#, has also written in great detail about whethervaris bad practice. In a nutshell, “no”.