Home
  Latest posts
  My Writings
  My Code
  My Gallery
  About me
 
  rssfeed Syndication
 
Bloggtoppen.se
 
 
Links
  Cornerstone
  SweNug
 
Post categories
  misc (47)
  Architecture (21)
  C# (19)
  Asp.Net (2)
  Vb.Net (2)
  Training (7)
  Data (19)
  Events (40)
  Platform (2)
  Orcas (4)
  Updates (3)
  Methods (10)
  Tools (6)
  Announcements (14)
  Languages (1)
  Patterns (6)
  Opinions (11)
  Fun (3)
  Ineta (1)
  Opinion (0)
  Practices (2)
  WCF (5)
 
 
 

C# 3.0 For .NET 2.0 applications

Saturday, March 29, 2008

How about using lambdas, anonymous types, type inference, automatic properties and lazy initialization in your .NET Framework 2.0 application? Using the multi targeting feature in Visual Studio 2008 will help you do that. With Multi-Targeting in VS 2008 can build application for .NET Framework 2.0 -> 3.5 by just setting a project property to inform VS what version to target.

Selecting the target framework will compile the application to run on the specified version. This makes it quite easy to upgrade from VS2005 to VS2008 since you don't have to move your codebase into V3.5.

Note: While the application will run on .NET Framework 2.0 the project files will be specifically for VS2008, so if the developers in your team is upgrading, makes sure all of them can before letting anyone convert the project files.

This is a really nice feature in VS2008. But what does that has to do with the title of this post?

When investigating how visual studio does the multi targeting one of the first thing you will notice is that Visual Studio is using the CSC compiler from v3.5 of the framework:

There is no special flag sent to CSC to tell it to compile for V2 it just compiles. This works since the real difference between V2 and V3.5 lies in the class libraries and not in the CLR / CLI specifications. Both versions of the framework shares V2.0.50727 (3.5 just adds a service pack to it) and when building apps for V3.5 we event get the base class library mainly from V2 so mscorlib and it's siblings is not upgraded to V3.5.

The effect of this is that you for the projects you upgrade from VS2005 or applications you build that will target V2; will be able to use C# 3.0 features since they are all compiler tricks. Well almost all of them, extension methods require a reference to System.Core.dll which isn't available when targeting V2 of the framework. I've tried and confirmed that lambdas, anonymous types, type inference, automatic properties and lazy initialization will work fine for your V2 applications using Visual Studio 2008.

So if the CLR and the compiler is indifferent, what does differ from V2 and V3.5? It's really about what class libraries are shipped with the different version, targeting V2 Visual Studio will filter out the assembles that won't be available on a V2 installation. 

So upgrading to VS2008 will have more benefits then just nice new colors and some new analyzing tools if your building applications for V2 of the Framework, you will also get some of those nice productivity enhancement that C# 3.0 brings to the table.

Now are there any reason not to upgrade at this point?

 

Comments
3/29/2008 4:19:00 PM   http://www.rogeralsing.com   -   Roger Alsing
 
You can get extension methods to work under 2.0 too.

You have to add a file to your solution with the following code:

namespace System.Runtime.CompilerServices
{
public class ExtensionAttribute : Attribute
{
}
}

Thats it :-)

You can now use _your own_ extension methods.

The ones that are shipped with .NET3.5 are not available since that code is not present in the 2.0 BCL.
 
3/29/2008 4:28:00 PM   https://lowendahl.net   -   Patrik Löwendahl
 
Thanx Roger, I just figured that out and posted a blog post on that :) Now since Query Expression are converted into calls to extension methods, it should be possible to get LINQ to qork as well.
 
3/29/2008 10:38:00 PM   http://www.rogeralsing.com   -   Roger Alsing
 
I've started a little bit on an in mem Linq engine for the .NET 2.0 runtime.

www.puzzleframework.com/Roger/LinqForNet2.zip

Far from complete but it handles the most common Linq constructs and supports deferred execution.

//Roger
 
3/31/2008 9:31:00 PM   http://andreasohlund.blogspot.com/   -   Andreas Öhlund
 
Unfortunately if you develop biztalk solutions you have a reason not to upgrade :(

VS2008 doesn't supportBiztalk projects...

http://geekswithblogs.net/cyoung/archive/2007/12/01/117279.aspx
 
4/1/2008 10:57:00 PM   http://blogical.se/blogs/johan   -   Johan Hedberg
 
As for BizTalk, you should be able to do pipeline component, functoid and helper class development in VS.NET 2008 as long as you target the 2.0 runtime, since those are just regular library projects, and require none of the visual designers or project templates. For the latter you're stuck with 2005.
 
4/4/2008 6:50:00 PM   http://musingmarc.blogspot.com   -   Marc Brooks
 
A much more complete Linq for 2.0 is the LinqBridge here: http://www.albahari.com/nutshell/extras.html

I also strongly recommend the awesome LinqPad and their book...


Comment
Title:
Your name:
Your url:
Text:
Please enter the text from the image: