"() =>" 在 C# 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/630330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:02:29  来源:igfitidea点击:

What does "() =>" mean in C#?

c#syntaxlambda

提问by Edward Tanguay

Came across the following line in the Composite Application Guidelines.

在复合应用程序指南中遇到以下行。

I know the =>is a lambda but what does the ()mean?

我知道=>是一个 lambda 但()是什么意思?

What are some other examples of this?

这方面还有哪些其他例子?

What is it called so I can search for it?

它叫什么,我可以搜索它?

this.regionViewRegistry.RegisterViewWithRegion(RegionNames.SelectionRegion
        , () => this.container.Resolve<EmployeesListPresenter>().View);

采纳答案by Ward Werbrouck

It's a lambda expression that takes 0 arguments

这是一个带 0 个参数的 lambda 表达式

http://msdn.microsoft.com/en-us/library/bb397687.aspx

http://msdn.microsoft.com/en-us/library/bb397687.aspx

回答by Alex Fort

That's an empty argument list, meaning the lambda expression takes no arguments.

这是一个空的参数列表,这意味着 lambda 表达式没有参数。

回答by Mendelt

If you look at x => x + 1

如果你看 x => x + 1

It takes a parameter x and returns x incremented by one. The compiler will use type inference to deduct that x is probably of type int and will return another int so you have a lambda that takes a parameter x of type int and returns an integer.

它接受一个参数 x 并返回 x 递增一。编译器将使用类型推断来推断 x 可能是 int 类型,并将返回另一个 int,因此您有一个 lambda,它采用 int 类型的参数 x 并返回一个整数。

() => 3;

is the same but doesn't take a parameter, it will return an integer.

相同但不带参数,它将返回一个整数。

() => Console.WriteLine("hello");

Will result in a void method with no parameters.

将导致没有参数的 void 方法。