Tuesday, May 27, 2008 8:58 PM
by
benmoore
C# - to var or not to var?
After a discussion about ReSharper with yet another colleague the other day, I decided that it was finally time I installed the trial and gave it a go. After all, it's only on version 4 already :)
I've un-installed it after only a day or so, I won't go into that right now, but during the time I had it installed it kept prompting me to change the way I declared variables. Every time I declared a variable like this
string myValue = "Hello";
I would get a prompt suggesting I replaced the declaration with
var myValue = "Hello";
This prompt appeared for any local variable declarations, but I find myself questioning whether this is good coding practice or not. I understand the benefits of the var keyword and in certain scenarios, such as when using LINQ or anything else that uses anonymous types, it is an extremely powerful tool to have at your disposal. However, in plain old coding, surely you want to be explicit about what you are declaring? The two expressions may well get compiled down to the same code, but in a world where code readability and maintainability rank highly on the priority list then I personally like being able to see at a glance what types I'm dealing with.
Just my 2p...