Tuesday, June 17, 2008

Using C# 3.0 in ASP.NET 2.0

C# 3.0 has a lot of great features to help with ASP.NET 3.5 web projects. However, you can also use SOME C# 3.0 features in SOME .NET 3.0 and 2.0 projects if you're using Visual Studio 2008. The gist of it is, if you create a new "ASP.NET Web Application" solution (File -> New -> Project... -> Web -> ASP.NET Web Application) you can use most C# 3.0 features (lambdas, vars, anonymous types, etc.) The exception being LINQ, expression trees and extension methods, which require additional classes/attributes that are only present in the 3.5 base class library. This works because the compiler is generating all of the code for your site into DLLs which have to be deployed along with your pages, and .NET 2.0, 3.0 and 3.5 all target the .NET 2.0 CLR.

However, if you are creating an "ASP.NET Web Site" solution (File -> New -> Web Site... -> ASP.NET Web Site), all bets are off. With this type of solution, you are relying on the framework to compile your application at run time. Thus, the target framework's C# compiler will be used. Because the C# 3.0 compiler is only available in .NET 3.5, you can not use C# 3.0 features with this feature of ASP.NET prior to v3.5.

There are a few ways around these limitations. First, you can use LinqBridge. LinqBridge recreates several of the classes from the 3.5 framework in the 2.0 framework, effectively letting you use nearly all C# 3.0 features in your .NET 2.0 projects. Expression trees are the only feature you won't be able to use. Extension methods, LINQ to Objects and query expressions all work just fine. However, you must be using the C# 3.0 compiler to use LinqBridge, so ASP.NET Web Site solutions are still left out in the cold.

Almost.

It turns out there is a sort of work around to compile an ASP.NET Web Site solution with C# 3.0, as long as you don't mind sacarificing dynamic compilation. From the Website menu, select "Start Options". Click on "Build". Change the Target Framework to 3.5 and clear the "Build Web site as part of solution" checkbox and click OK. Now click on "MSBuild Options" and set "Output Folder" to a path outside of your solution's source tree. I'd like to say I know why you have to do that, but I don't. You'll get an error from MSBUild if you specify an output folder within your solution's source tree. Boo. Hiss. Moving on. At this point, you will no longer be able to build your project in Visual Studio. However, you can build it with MSBuild. If you fire up a VS2008 command prompt, change to your web site solution's folder and type "msbuild", your project will compile with the C# 3.0 compiler. Please note that you are compiling your web site to DLLs, so you will not be able to make code updates. Still, it's potentially a nice interim solution until you can move your web projects over.

Here's a feature capability table:

Solution TypeProjectWeb Site
.NET Framework Version3.53.02.02.0 with LinqBridge3.53.02.0
Expression TreesYNNNYNN
LINQ/Query ExpressionsYNNYYNN
Extension MethodsYNNYYNN
LambdasYYYYYNN
"var" keywordYYYYYNN
Anonymous TypesYYYYYNN
Automatic PropertiesYYYYYNN
Object InitializersYYYYYNN
Collection InitializersYYYYYNN