views:

829

answers:

3

If I have Visual Studio 2008 and I target a .NET 2.0 application, can I still use Lambda Expressions? My understanding of Lambda Expressios is that its a feature built into the compiler, not the framework, so my conclusion would be that I could use Lambda in .NET 2.0 application. Can someone please tell me if this is so?

A: 

It does not work. Using Linq requires the System.Linq to be part of the framework assembly, which .NET 2.0 does not have.

DanDan
yep, dandan's right. Also, no, you can't reference the 3.5 assemblies by just putting them in your bin. I've asked...the problem is there's been a lot of changes in the System namespace too.
andy
This is not the case. LINQ is most certainly supported using a 3.5 compiler (Vb9) to target a 2.0 application provided you have sufficiently available LINQ methods such as Select and Where. True for C# as well
JaredPar
+1  A: 

Yes you are correct. You can use lambda expressions in place of anonymous methods. The compiler will sort the rest out. Try this:

int sum = 0;
Array.ForEach(new[] {1, 2, 3, 4}, x => sum += x);

What you cannot do is to use any of the new functionality of .Net 3.5 (ie. Linq). Doing so requires adding references to System.Linq, System.Core,etc.., which are not present in .Net 2.0.

adrianbanks
+9  A: 

Yes this is completely supported. As long as you do not build an expression tree or otherwise reference System.Core, System.Xml.Linq, etc ... it is perfectly legal to use Lambda expressions in a down targetted 2.0 application. This is true of any other compiler feature introduced in VS2008 (VB9).

EDIT

Several answers incorrectly state that Lambda Expressions are a feature of the 3.5 or 3.0 feature. Lambda expressions are a compiler feature not a Framework one. They require no framework support in order to function and it's perfectly legal to use them in a application down targetted to 2.0.

The only place you would get into trouble is if you used a lambda as an expression tree. Expression Trees are both a compiler and framework feature and do require 3.5 to function correctly. But you have to work hard to enable this as we actively try to prevent it from happening.

JaredPar