()的含义=> C# 中的运算符(如果存在)

发布于 2024-09-18 03:42:18 字数 323 浏览 6 评论 0原文

我在 Jon Skeet 的回答中此处读到了这一有趣的行。

有趣的是,他提倡使用委托:

Log.Info("I did something: {0}", () => action.GenerateDescription());

问题是,这 ()=> 是什么?运营商,我想知道?我尝试用谷歌搜索它,但由于它是由符号组成的,所以谷歌实际上并没有多大帮助。我在这里尴尬地错过了什么吗?

I read this interesting line here, in an answer by Jon Skeet.

The interesting line is this, where he advocated using a delegate:

Log.Info("I did something: {0}", () => action.GenerateDescription());

Question is, what is this ()=> operator, I wonder? I tried Googling it but since it's made of symbols Google couldn't be of much help, really. Did I embarrassingly miss something here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

烟雨扶苏 2024-09-25 03:42:19

这引入了一个不带参数的 lambda 函数(匿名委托),它相当于并且基本上是简写:

delegate void () { return action.GenerateDescription(); }

您还可以添加参数,因此:

(a, b) => a + b

这大致相当于:

delegate int (int a, int b) { return a + b; }

This introduces a lambda function (anonymous delegate) with no parameters, it's equivalent to and basically short-hand for:

delegate void () { return action.GenerateDescription(); }

You can also add parameters, so:

(a, b) => a + b

This is roughly equivalent to:

delegate int (int a, int b) { return a + b; }
杯别 2024-09-25 03:42:19

=> 这是 lambda 运算符。当我们没有任何输入参数时,我们只需在 lambda 运算符之前使用圆括号 () 即可。

句法:
(输入参数)=>表达式

=> this is lambda operator. When we don't have any input parameters we just use round brackets () before lambda operator.

syntax:
(input parameters) => expression

被你宠の有点坏 2024-09-25 03:42:19

创建指定方法的匿名委托。

也许,在你的情况下,它将是一个 Func

Creating an anonymous delegate to specified method.

Probably, in your case it will be a Func<string>

你在我安 2024-09-25 03:42:19

这是 lambda 表达式的示例,您可以在此处了解更多信息。

This is an example of a lambda expression you can learn more here.

阳光下的泡沫是彩色的 2024-09-25 03:42:19

这是将不带参数的匿名委托作为 lambda 表达式传递的方法。

与 .NET 2.0 类似

Log.Info("I did something: {0}", delegate()
            {
                return action.GenerateDescription();
            });

It's way to pass anonymous delegate without parameters as lambda expression.

Similar to this from .NET 2.0

Log.Info("I did something: {0}", delegate()
            {
                return action.GenerateDescription();
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文