TOP KEK
TOP KEK

Reputation: 2641

How does this lambda function work?

I have a function with this signature:

public DeleteCommand(IService service, 
    Func<bool> canExecute, Action<ContactModel> deleted)

and code which calls it:

Delete = new DeleteCommand(
                Service, 
                ()=>CanDelete,

I don't understand what ()=>CanDelete exactly means. Being a Func<bool> it must return some value.

()=> 

means it has no input parameters. But what is the value returned? why there is no return in the lambda? Something like ()=> return CanDelete?

Upvotes: 1

Views: 72

Answers (1)

Servy
Servy

Reputation: 203802

An expression lambda, which is what you have shown, returns the result of the expression following the =>. The return keyword is inferred, and in fact cannot be explicitly included. A statement lambda (which is of the form () => { someStatements;}) does not infer a return value, and requires an explicit return if it is not void.

Upvotes: 9

Related Questions