Olivier Muhring

Olivier Muhring

  • NA
  • 150
  • 8.8k

To var or not to var

Jun 15 2012 10:52 AM
Since .NET 3.5 the keyword "var" was introduced into the C# language. I used to think it was an oddity, mostly used by programmers who don't feel like declaring all their variables at the beginning of a methods, or simply liked to use it as  a "shortcut" in some situations. Most of the examples I saw over the years where just that.

Recently, however, I've started to use Resharper, to assist me in my development projects. And Resharper continuously suggests to replace my declared variables with var constructs.

Hence my question: to var, or not to var?  When do I use var, and when do I not. Is there a rule of thumb, or just a simple personal preference?

Answers (2)