Reputation: 15455
What does = () =>
mean in c#?
I've used lambda's before but those empty parens ()
are throwing me off.
Familiar with this:
customers.Find(x=>x.FirstName=="John")
Upvotes: 0
Views: 81
Reputation: 5314
The ()
simply means the anonymous method has no parameters. The way you're used to seeing, like customers.Find(x=>x.FirstName == "John")
is the same... the first x
is the parameter passed to the lambda. The parentheses are optional if there's only a single parameter, so this could also be written like this: customers.Find((x)=>x.FirstName == "John")
With a method that takes no parameters, the 'single parameter' exclusion doesn't apply, so you have to write the ()
. You can see more in the documentation.
The =
before the lambda call is assigning the method body that follows to the Implementation
property.
Upvotes: 2
Reputation: 3785
The () => new Sequence
part along with the block below it is an lambda function that takes no parameters and return a Sequence
This lambda is assigned to this.Implementation
so that at a later time you can call the lambda. E.g., var s = this.Implementation()
.
Upvotes: 2
Reputation: 8551
It's assigning a lambda expression to the variable or property this.Implementation. You have to break down the operators like this:
this.Implementation
= //assignment operator
()=> new Sequence { /* stuff */ };
The () is to designate that the method takes no parameters; the => identifies what follows as the code to be run when the lambda is invoked.
Upvotes: 4
Reputation: 14515
This is known as a lambda expression. In essence, it's shorthand for defining a function.
Here is a decent tutorial explaining the concept:
http://www.dotnetperls.com/lambda
Upvotes: 2