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

2019-03-24 15:57发布

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

标签: c# syntax lambda
3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-24 16:24

It's a lambda expression that takes 0 arguments

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

查看更多
做自己的国王
3楼-- · 2019-03-24 16:24

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.

查看更多
叼着烟拽天下
4楼-- · 2019-03-24 16:24

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

查看更多
登录 后发表回答