Another technology blog...

Why ReSharper recommends the “var” keyword in .NET 2.0 projects

Sunday, January 24, 2010

I was a little confused this week as to why ReSharper was recommending using implicitly typed variable declarations in a VS2010 solution targeting .NET 2. Somewhere in my mind I had directly associated the “var” keyword with the release of .NET 3.5 so this looked a little odd to me:

image

image

As it turns out, the var keyword is a feature of the compiler, not the .NET CLR. The same is true for automatic properties and object initialisers. The bottom line is that you can use these features in VS08 or VS2010 and the compiler will happily go along with it and translate the code to .NET 2.0 compatible syntax in the object code.

There’s an excellent post on Shahar Gvirtz's blog where he disassembles code using this syntax in Reflector to reveal plain old .NET 2.0 syntax. So in short, implicit typing is fine for anyone running a recent version of Visual Studio and, as usual, ReSharper is correct in identifying this as an opportunity to polish the code.


Share/Save/Bookmark

0 comments:

Post a Comment

Disclaimer

Opinions expressed here are my own and may not reflect those of my employer, my colleagues, my mates, my wife and so on and so forth. Unless I’m quoting someone, they’re my own opinions and may not necessarily be cohesive nor entertaining but hey, at least they’re original!