C# 内联 lambda 计算

发布于 2024-10-13 10:07:25 字数 641 浏览 1 评论 0原文

在使用 C# 编程时,我曾多次发现自己想要定义 lambda(或匿名委托)并在同一行中调用它。在这一点上,我能够做到这一点的“最干净”的方式是这样的:

bool foo_equals_bar = new Func<String, bool>(str => str.Equals("foo"))("bar");

我希望能够写出如下所示的内容:

bool foo_equals_bar = (str => str.Equals("foo"))("bar");

不幸的是,这似乎不起作用。我很想知道:

  1. 是否有更简单的方法来编写上面的代码行?
  2. (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:

  1. Is there a simpler way of writing the line of code above?
  2. What is returned from (str => str.Equals("foo")) such that is can be used to initialize a Func<String, bool>, but can not be evaluated like a Func<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谈场末日恋爱 2024-10-20 10:07:25

您需要一组辅助方法来使编译器推断 lambda 类型,例如:


public static class Functional
{
    public static Func<TResult> Lambda<TResult>(Func<TResult> func)
    {
        return func;
    }

    public static Func<T, TResult> Lambda<T, TResult>(Func<T, TResult> func)
    {
        return func;
    }

    public static Func<T1, T2, TResult> Lambda<T1, T2, TResult>(Func<T1, T2, TResult> func)
    {
        return func;
    }
}

现在您可以编写:


bool foo_equals_bar = Functional.Lambda(str => str.Equals("foo"))("bar");

You would need a set of helper methods to make compiler infer lambda types, e.g.:


public static class Functional
{
    public static Func<TResult> Lambda<TResult>(Func<TResult> func)
    {
        return func;
    }

    public static Func<T, TResult> Lambda<T, TResult>(Func<T, TResult> func)
    {
        return func;
    }

    public static Func<T1, T2, TResult> Lambda<T1, T2, TResult>(Func<T1, T2, TResult> func)
    {
        return func;
    }
}

Now you can write:


bool foo_equals_bar = Functional.Lambda(str => str.Equals("foo"))("bar");

以可爱出名 2024-10-20 10:07:25
str => str == "A" 

与相同

delegate (string str) { return str == "A";};

所以不,没有办法只获取 lambda,因为如果您只是说

bool result = (str => str == "A")("B");

编辑,编译器将不知道 str 是什么类型:

是的,您可以向 lambda 表达式添加类型,例如 (string str )=> str == "A"; 但由于某种原因它们仍然不能是隐式的。不知道为什么。感谢您的评论,尤里。

str => str == "A" 

is the same as

delegate (string str) { return str == "A";};

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

bool result = (str => str == "A")("B");

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.

国产ˉ祖宗 2024-10-20 10:07:25

(str => 返回什么
str.Equals("foo")) 这样可以是
用于初始化 Func,但不能像
功能?

仅使用内置类型,就有:

Expression<Func<string, bool>> a = str => str.Equals("foo");
Expression<Predicate<string>> b = str => str.Equals("foo");
Expression<Action<string>> c = str => str.Equals("foo");
Func<string, bool> a1 = str => str.Equals("foo");
Predicate<string> b1 = str => str.Equals("foo");
Action<string> c1 = str => str.Equals("foo");

所有这些都是 lambda 表达式的有效解释。这只是我能立即想到的内置类型。还有与签名匹配的任何委托。

What is returned from (str =>
str.Equals("foo")) such that is can be
used to initialize a Func, but can not be evaluated like a
Func?

Just using built-in types, there's:

Expression<Func<string, bool>> a = str => str.Equals("foo");
Expression<Predicate<string>> b = str => str.Equals("foo");
Expression<Action<string>> c = str => str.Equals("foo");
Func<string, bool> a1 = str => str.Equals("foo");
Predicate<string> b1 = str => str.Equals("foo");
Action<string> c1 = str => str.Equals("foo");

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.

楠木可依 2024-10-20 10:07:25

在编程时的不同时间
C# 我发现自己陷入了困境
我想在其中定义 lambda (或
匿名委托)并在
同一条线。

这些都是奇怪的情况。避开他们。这是不可能的,因为编译器无法推断类型。当您将匿名委托传递给编译器可以从中推断类型的某个方法时,这可能会起作用。

在任何情况下:

bool foo_equals_bar = "bar".Equals("foo");

都比 * 更具可读性:

bool foo_equals_bar = (str => str.Equals("foo"))("bar");

* 假设这样的语法是可能的

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.

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:

bool foo_equals_bar = "bar".Equals("foo");

is far more readable than*:

bool foo_equals_bar = (str => str.Equals("foo"))("bar");

* assuming such syntax was possible

一抹淡然 2024-10-20 10:07:25

您必须将 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")

浅浅淡淡 2024-10-20 10:07:25
using System.Threading.Tasks;
using System.Threading;

Task.Run(()=>{
     //do something
});

您可以添加 500 毫秒的延迟,

Task.Run(()=>{
    Thread.Sleep(500);
    //do something
});

不确定 MS 何时添加它,但它不是 C#3,

这种编程风格可以完成任何事情,一旦您了解了这一点,您基本上可以忽略其余的 C# 规范

using System.Threading.Tasks;
using System.Threading;

Task.Run(()=>{
     //do something
});

You can add a 500ms delay with

Task.Run(()=>{
    Thread.Sleep(500);
    //do something
});

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文