Lamba表达式无法按预期使用 [英] Lamba expression cannot be used as expected
问题描述
下面的代码产生编译器错误期望表达"(带下划线的子句).
The following code produces the compiler error "Expression expected" (Sub is underlined).
Dim lambda As Action(Of Integer) = Sub(x) Console.WriteLine(x)
为什么这不起作用?
注意:相应的C#代码有效:
Note: The corresponding C# code works:
Action<int> lambda = x => Console.WriteLine(x);
推荐答案
我猜您正在使用Visual Studio2008.VB.NET中的操作lambda((Sub
关键字)
I guess you're using Visual Studio 2008. Action lambdas in VB.NET (Sub
keyword) have been added in VB 10, which ships with Visual Studio 2010. The same is true for multi-line lambdas, by the way.
请注意,这不是框架问题,而是编译器问题:只要使用VS 2010编译器,即使在.NET 3.5项目中也可以使用动作lambda.
Note that this is not a framework issue, but a compiler issue: You can use action lambdas even in .NET 3.5 projects, as long as you use the VS 2010 compiler.
C#在VB之前有动作lambda,这就是为什么C#示例即使在VS 2008中也可以工作的原因.
C# had action lambdas before VB, which is why the C# example works even in VS 2008.
这篇关于Lamba表达式无法按预期使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!