Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Lambda Expressions

If I have Visual Studio 2008 and I target a .NET 2.0 application, can I still use Lambda Expressions? My understanding of Lambda Expressions 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?

like image 971
Icemanind Avatar asked Jul 21 '09 22:07

Icemanind


People also ask

What is lambda expression in VB net?

A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid. Lambda expressions can be functions or subroutines and can be single-line or multi-line. You can pass values from the current scope to a lambda expression.

What is LINQ and lambda expressions?

The term 'Lambda expression' has derived its name from 'lambda' calculus which in turn is a mathematical notation applied for defining functions. Lambda expressions as a LINQ equation's executable part translate logic in a way at run time so it can pass on to the data source conveniently.


2 Answers

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.

like image 96
JaredPar Avatar answered Oct 21 '22 09:10

JaredPar


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.

like image 29
adrianbanks Avatar answered Oct 21 '22 10:10

adrianbanks