Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

syntax

c#

lambda

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);
like image 224
Edward Tanguay Avatar asked Mar 10 '09 14:03

Edward Tanguay


3 Answers

It's a lambda expression that takes 0 arguments

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

like image 66
Ward Werbrouck Avatar answered Nov 06 '22 07:11

Ward Werbrouck


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.

like image 44
Mendelt Avatar answered Nov 06 '22 09:11

Mendelt


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

like image 28
Alex Fort Avatar answered Nov 06 '22 08:11

Alex Fort