What does “() =>” mean in C#?

2019-03-24 16:05发布

问题:

Came across the following line in the Composite Application Guidelines.

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

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);

回答1:

It's a lambda expression that takes 0 arguments

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



回答2:

If you look at 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.

() => 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.



回答3:

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



标签: c# syntax lambda