Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application

Did you know that if you are using Visual Studio 2008 but targeting the .Net 2.0 framework you can still use some of the new syntax features of .Net 3.5 in your applications?  That's right, you read that correctly you too can be a cool kid while still working on your old dilapidated, buggy legacy application (ok, maybe using the new syntax will not make you a cool kid... I tried).

Anyway, turns out you can use the new syntax features just as long as you do not reference the new .Net 3.5 assemblies.

The new assemblies are:
System.Data.Linq
System.Xml.Linq
System.Core

New Syntax/Features you can use is:
Var keyword
Lambda statements
Extension Methods (kinda, sort, you need to read this for more information)
Object Initializers
Anonymous Types  (Moved from the cannot use section, thanks Jon)

New Syntax/Features you cannot use is:
Linq (any variant of Linq)
Query Extensions
Extension Methods (see the link above for more information)

Now you may be asking, how can you use the new .Net 3.5 stuff when targeting the .Net 2.0 framework?  Well, simple all the allowed new stuff is really nothing more than compiler tricks.  The 'Var' keyword really is just replaced at compile time with the strongly typed equivalent.  Lambda's are really nothing more then a fancy new facade on anonymous delegates.  And Object Initializers are just ways to reduce the number of lines you right.

So, if you are like me and stuck in a .Net 2.0 project but would like to use some of the new features and you are using VS 2008, have at it.  Just make double sure that you are 1) targeting the .net 2.0 framework 2) you do no reference any of the new assemblies that are part of .Net 3.5.

Till next time,

[----- Remember to check out DimeCasts.Net -----]



Comments

Jon Skeet said:

Um, you *can* use anonymous methods (they were introduced in C# 2). (If you meant anonymous types, you can use those too.)

You can also use LINQ to Objects (including query expression syntax) if you use something like LINQBridge.

Basically the only thing you can't use at all is expression trees, and hence any out-of-process LINQ.

See csharpindepth.com/.../Versions.aspx

for more info.

# June 16, 2008 7:17 AM

Derik Whittaker said:

@Jon,

Thanks for the clarification on my typo (I changed the wording from methods to types).

Also, thanks for the link to your great book that talks about C#.

# June 16, 2008 8:15 AM

Tom Clarkson said:

Don't forget 3) your client doesn't mind not being able to open the project in VS2005.

Definitely nice to have the new compiler features if you can get away with it though.

# June 16, 2008 9:52 AM

Tyrone said:

I'm not sure why you are stuck on the 2.0 framework. It's not like installing 3.5 cost anything or break your app. What are the reasons why you can't move to 3.5?

# June 16, 2008 10:00 AM

Derik Whittaker said:

@Tyrone,

Easy

1) sometimes the business does not want to take the time/risk to do an upgrade (even though we know it is very low risk)

2) in my case they are running 3 web applications on one server farm (i know, that is bad) and they need to do a full QA cycle with the .Net 3.5 framework before they will upgrade production.

# June 16, 2008 10:05 AM

Jon Skeet said:

Derik: You've corrected it to "Anonymous types" but left it in the "can't use" section - you can use them with no problem :)

# June 16, 2008 10:28 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# June 16, 2008 11:03 AM

Dew Droplet – June 16, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Droplet – June 16, 2008 | Alvin Ashcraft's Morning Dew

# June 16, 2008 11:15 AM

Steve Hiner said:

Tyrone:  For me it's Windows 2000 support. My client requires it, .NET 3.x doesn't have it.  Despite this being a new app I'm locked into 2.0 for the moment.

Another reason is not making your customers install yet another huge library.  That's why one of my apps is still using 1.1.  I'd love to move it to 2.0 or 3.x but at least half my users don't have 2.0 installed and I'd rather not make them spend time downloading it then 30 or 45 minutes installing it just to get an update to my software.

# June 16, 2008 5:09 PM

GrillerGeek said:

This does not appear to be true for 2.0 Websites though, at least it did not work for me when I tried to use the var keyword.

# June 17, 2008 9:38 AM

С# 3.0 и .NET 2.0 | АяксЛайн.ру said:

Pingback from  С# 3.0 и .NET 2.0 | АяксЛайн.ру

# June 17, 2008 9:03 PM

wwfDev said:

Also, I guess you can use the new short-cuts for properties (as in public string Name { get; set; } )?

# June 18, 2008 2:17 AM

Arjan`s World » LINKBLOG for June 18, 2008 said:

Pingback from  Arjan`s World    » LINKBLOG for June 18, 2008

# June 18, 2008 10:40 AM

jetbra said:

ReSharper 4.0 will keep you straight as it supports multitargeting in VS2008.  I let it drive me and the new C# 3.0 features on .NET 2.0

# June 19, 2008 1:26 AM

oldschool said:

.NET 3.5 doesn't run on Windows 2000.  Funny as it is, there are many Windows 2000 machines still in production.

# June 19, 2008 1:30 AM

Blue Onion Software - Onion Peels Blog - Friday Links #6 said:

Pingback from  Blue Onion Software - Onion Peels Blog - Friday Links #6

# June 20, 2008 8:59 AM

Tramadol. said:

Buy tramadol index. Tramadol.

# July 26, 2008 3:28 PM

Fioricet cheap. said:

Fioricet. Buy fioricet.

# August 7, 2008 3:06 AM

World of .NET show said:

Podcast #1 - Review of CSLA, ReSharper, Using C# 3.0 syntax in a .NET 2.0 application, Domain Driven Design book

# August 22, 2008 12:47 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News