VB.NET Lambda Expressions

天大地大妈咪最大 提交于 2019-12-23 10:09:10

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/1162184/vb-net-lambda-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!