The C# 3.0 Compiler vs. .NET 2.0 Projects
Hello, Elegant Readers - to help me figure out how WordPress is put together, I thought I’d write a quick blog post about a question that came up last week. Recently this situation was discussed: someone had a solution that compiled and ran just fine on Visual Studio 2005, but after they upgraded it to Visual Studio 2008, targeting the .NET 2.0 framework, they ran into some major errors.
Comparing the VS2005 vs VS2008 files showed that the only thing changed in those files was the “solution version” info from 8 to 9 - just the basic “you’re a now VS2008 project” information, but nothing vital. So, if nothing changed in the files, and we’re still targeting the .NET 2.0 framework, why would the program break? Shouldn’t the VS2008 output be identical to VS2005?
The answer is: nope.
Even if you are targeting the .NET 2.0 framework, Visual Studio 2008 will still use the C# 3.0 compiler and those .NET 3.5-based tools to do it’s thing. In fact, you can even use all of that C# 3.0 syntax in your .NET 2.0-targeted projects without any problems: so “var,” anonymous types and properties, and lambda expressions all can be used. The C# 3.0 compiler just generates standard 2.0 IL for those things.
The only thing you can’t do is use those .NET 3.0 / 3.5 features that rely on libraries provided by the newer .NET versions - LINQ, Func<T>, all that functionality that is included in the newer assemblies - well, you have to have the assemblies for that to work.
The project I used to see this for myself is attached - sometimes the best way to understand something is to do some empirical research, boil an issue down to something that you could test, write a prototype for, or just slap a new project together.







Interesting post, although it would be nice to hear more about what went wrong with that first project you referenced. I’m not sure I see why c# 2.0 code would go wrong when compiled as 3.0, especially if you’re not using the 3.5 framework.
That’s a good question - I will see if I can get more information out of person who reported the original problem. It would be interesting to see what they were trying to do…
[...] [...]