Just Use var All The Time

In talking of making C# a little less verbose I can’t really avoid the “var” keyword

There is no arguing with the fact that it makes code less verbose.

Dictionary<string, Person> people = new Dictionary<string, Person>();
/* OR */
var people = new Dictionary<string, Person>();

The ReSharper guys have the best write up of it: http://resharper.blogspot.com/2008/03/varification-using-implicitly-typed.html

Now I will concede that there may be a few edge cases were “var” slightly decreases readability.

However the brain power required to decide when you have one of these cases means the increased readability is not worth it.

So: Just use var all the time.

In fact set “non var” usages to errors in ReSharper (If you use it)

Resharper > Options > Inspection Severity > Language Usage Opportunities

  • Use 'var' keyword when initializer explicitly declares type
  • Use 'var' keyword when possible
Posted by: Simon Cropp
Last revised: 20 Dec, 2011 07:38 PM History

Comments

No comments yet. Be the first!

No new comments are allowed on this post.