Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing lambda expression as function parameter

Tags:

c#

lambda

Function like bool decide(bool x) can be passed in method as parameter as functor as:

foo(Func<bool,bool> lambda)

We can have lambda expression like ()=>{int x=8; x=x+2;} that does not take anything and return anything. Lets say I want to pass such function as parameter to another method bar then how can it be done?

like image 332
user3 Avatar asked Mar 27 '26 19:03

user3


1 Answers

This is Action, not Func. If you don't want to return value, then you must use Action.

For example:

Action<int> example1 = (int x) => Console.WriteLine("Write {0}", x);
example1.Invoke(1); // or example1(1);

Action example3 = () => Console.WriteLine("Done");
example3.Invoke(); // or example3();
like image 179
Farhad Jabiyev Avatar answered Mar 29 '26 08:03

Farhad Jabiyev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!