简体   繁体   中英

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

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

It's a lambda expression that takes 0 arguments

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM