C# 内联 lambda 计算
在使用 C# 编程时,我曾多次发现自己想要定义 lambda(或匿名委托)并在同一行中调用它。在这一点上,我能够做到这一点的“最干净”的方式是这样的:
bool foo_equals_bar = new Func<String, bool>(str => str.Equals("foo"))("bar");
我希望能够写出如下所示的内容:
bool foo_equals_bar = (str => str.Equals("foo"))("bar");
不幸的是,这似乎不起作用。我很想知道:
- 是否有更简单的方法来编写上面的代码行?
- 从
(str => str.Equals("foo"))
返回的内容可用于初始化Func
,但也可以不能像Func
一样进行计算?
我应该指出,我正在使用 C# 3 (VS2008),因此如果解决方案仅存在于 C# 4 中,请提及。 (我仍然想知道,即使目前我无法找到解决方案)。
谢谢
At various times while programming in C# I've found myself in situations where I'd like to define a lambda (or anonymous delegate) and call it in the same line. At this point, the 'cleanest' way I've been able to do this is like this:
bool foo_equals_bar = new Func<String, bool>(str => str.Equals("foo"))("bar");
I would love to be able to do write something like the following instead:
bool foo_equals_bar = (str => str.Equals("foo"))("bar");
Unfortunately, this doesn't seem to work. I would love to know:
- Is there a simpler way of writing the line of code above?
- What is returned from
(str => str.Equals("foo"))
such that is can be used to initialize aFunc<String, bool>
, but can not be evaluated like aFunc<String, bool>
?
I should point out that I'm working in C# 3 (VS2008), so if a solution only exists in C# 4, please mention that. (I'd still like to know, even if the solution isn't available to me at the moment).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要一组辅助方法来使编译器推断 lambda 类型,例如:
现在您可以编写:
You would need a set of helper methods to make compiler infer lambda types, e.g.:
Now you can write:
与相同
所以不,没有办法只获取 lambda,因为如果您只是说
编辑,编译器将不知道 str 是什么类型:
是的,您可以向 lambda 表达式添加类型,例如
(string str )=> str == "A";
但由于某种原因它们仍然不能是隐式的。不知道为什么。感谢您的评论,尤里。is the same as
So no, there's no way to get just the lambda, since the compiler wouldn't know what type str is if you just said
EDIT:
Yes, you can add types to lambda expressions, like
(string str) => str == "A";
but still, they can't be implicit for some reason. Not sure why. Thanks for the comment, Yuriy.仅使用内置类型,就有:
所有这些都是 lambda 表达式的有效解释。这只是我能立即想到的内置类型。还有与签名匹配的任何委托。
Just using built-in types, there's:
All of which are valid interpretations of the lambda expression. That's just the built-in types that I can think of off the top of my head. There's also any delegate that matches the signature.
这些都是奇怪的情况。避开他们。这是不可能的,因为编译器无法推断类型。当您将匿名委托传递给编译器可以从中推断类型的某个方法时,这可能会起作用。
在任何情况下:
都比 * 更具可读性:
* 假设这样的语法是可能的
Those are strange situations. Avoid them. This is not possible because the compiler cannot infer the type. This could work when you pass the anonymous delegate to some method from which the compiler can infer the types.
And in any cases:
is far more readable than*:
* assuming such syntax was possible
您必须将 lambda 转换为 func 或 delegate
((Func)(str => str.Equals("bar")))("baz")
You have to cast the lambda to a func or delegate
((Func<string, bool>)(str => str.Equals("bar")))("baz")
您可以添加 500 毫秒的延迟,
不确定 MS 何时添加它,但它不是 C#3,
这种编程风格可以完成任何事情,一旦您了解了这一点,您基本上可以忽略其余的 C# 规范
You can add a 500ms delay with
Not sure when MS added it but it wasn't C#3
this programming style can accomplish anything and you can basically ignore the rest of C# specs once you learn this