Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with Action<T1, T2> and passing multiple parameters

Tags:

c#

c#-4.0

I have this code:

        s(x => x.Open());

s is a method which calls one parameter, which is perfectly fine, like so:

 public void s(Action<p1> action) {}

Ignoring the naming conventions, if I make the method like the below:

 public void s(Action<p1, p2> action) {}

How do I pass in more than one parameter? Out of interest, is there any way to use the params keyword with Action<>?

Also, I am using C# 4.0 so I would be interested to see how it can help me in an way.

Thanks

like image 843
GurdeepS Avatar asked Dec 22 '22 04:12

GurdeepS


2 Answers

If you want to pass multiple parameters to a lambda expression in C# you need to enclose the parameters with parens. For example

s( (x,y) => x.Open(y) );
like image 53
JaredPar Avatar answered Jan 07 '23 14:01

JaredPar


s((x, y) => ...);
like image 35
Darin Dimitrov Avatar answered Jan 07 '23 13:01

Darin Dimitrov