VB.NET Lambda Expressions

2019-06-21 02:53发布

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?

3条回答
smile是对你的礼貌
2楼-- · 2019-06-21 03:44

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.

查看更多
老娘就宠你
3楼-- · 2019-06-21 03:49

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

查看更多
不美不萌又怎样
4楼-- · 2019-06-21 03:50

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.

查看更多
登录 后发表回答