What advantages does using var have over the explicit type in C#?

Corey Ogburn picture Corey Ogburn · Aug 6, 2010 · Viewed 16.7k times · Source

Possible Duplicates:
What’s the point of the var keyword?
Use of var keyword in C#

I understand how IEnumerable<...> for a datatype can make the code a little less readable or how nested generics can seem a little daunting. But aside from code readability, are there advantages to using var instead of the explicit type? It seems like by using the explicit type, you'd better convey what the variable is capable of because you know what it is.

If it's a workplace coding standard, I use it for the sake of teamwork. In my own projects however, I prefer to avoid the user of var.

Answer

Ben Robinson picture Ben Robinson · Aug 6, 2010

The point of var is to allow anonymous types, without it they would not be possible and that is the reason it exists. All other uses I consider to be lazy coding.