Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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 -----]


Posted 06-16-2008 5:57 AM by Derik Whittaker

[Advertisement]

Comments

Jon Skeet wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-16-2008 7:17 AM

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.

Derik Whittaker wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-16-2008 8:15 AM

@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#.

Tom Clarkson wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-16-2008 9:52 AM

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.

Tyrone wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-16-2008 10:00 AM

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?

Derik Whittaker wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-16-2008 10:05 AM

@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.

Jon Skeet wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-16-2008 10:28 AM

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

DotNetKicks.com wrote Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-16-2008 11:03 AM

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

Dew Droplet – June 16, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Droplet – June 16, 2008 | Alvin Ashcraft's Morning Dew
on 06-16-2008 11:15 AM

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

Steve Hiner wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-16-2008 5:09 PM

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.

GrillerGeek wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-17-2008 9:38 AM

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.

С# 3.0 и .NET 2.0 | АяксЛайн.ру wrote С# 3.0 и .NET 2.0 | АяксЛайн.ру
on 06-17-2008 9:03 PM

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

wwfDev wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-18-2008 2:17 AM

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

Arjan`s World » LINKBLOG for June 18, 2008 wrote Arjan`s World » LINKBLOG for June 18, 2008
on 06-18-2008 10:40 AM

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

jetbra wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-19-2008 1:26 AM

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

oldschool wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 06-19-2008 1:30 AM

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

Blue Onion Software - Onion Peels Blog - Friday Links #6 wrote Blue Onion Software - Onion Peels Blog - Friday Links #6
on 06-20-2008 8:59 AM

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

Tramadol. wrote Tramadol sr 100.
on 07-26-2008 3:28 PM

Buy tramadol index. Tramadol.

Fioricet cheap. wrote Fioricet.
on 08-07-2008 3:06 AM

Fioricet. Buy fioricet.

World of .NET show wrote Podcast #1 - Review of CSLA, ReSharper, Using C# 3.0 syntax in a .NET 2.0 application, Domain Driven Design book
on 08-22-2008 12:47 AM

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

World of .NET wrote Podcast #1 - Review of CSLA, ReSharper, Using C# 3.0 syntax in a .NET 2.0 application, Domain Driven Design book
on 04-09-2009 12:36 AM

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

Pharmd371 wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 10-11-2009 3:50 AM

Very nice site!

Pharmg284 wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 10-24-2009 2:04 PM

Very nice site!

Pharmd170 wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 10-24-2009 2:07 PM

Very nice site!

is wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 10-29-2009 6:23 AM

Hello!

<a href="docs.safehaus.org/.../~cheapvalium179 is valium used for</a> ,

effects wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 10-29-2009 6:26 AM

Hello!

<a href="docs.safehaus.org/.../~cheapambien527 effects of ambien cr</a> ,

overnight_ambien wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 10-31-2009 8:12 AM

Hello!

<a href="www.fysh.tcc.edu.tw/.../view.php ambien</a> ,

term wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 11-02-2009 4:06 PM

Hello!

<a href="forums.autosport.com/index.php term use of ambien</a> ,

snorting_valium wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 11-02-2009 4:08 PM

Hello!

<a href="forums.autosport.com/index.php valium</a> ,

laws wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 11-03-2009 5:17 AM

Hello!

<a href="vilinet.communityserver.com/.../137LowestPricesAMBIEN.aspx laws on levels of ambien in the blood</a> ,

Pharmg198 wrote re: Using C# 3.0 (.Net 3.5) syntax in a .Net 2.0 application
on 11-03-2009 7:20 PM

Very nice site!

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)