Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action delegate with more than four parameters (method arguments)

Tags:

c#

delegates

I have written a helper class which uses the Action - delegate as method parameter.
Like this:
public void SomeMethod(Action<T> methodToExecute, T argument);

According to the MSDN you can declare max. 4 arguments on an action delegate: Action<T1,T2,T3,T4>.

Now I'd like to call a method which needs 5! arguments. How could I do this?
The best solution would be something where I could use a dynamic number of method arguments.

Thanks

like image 228
TomTom Avatar asked Sep 11 '09 11:09

TomTom


People also ask

Can a delegate point to more than 1 method?

A delegate is a type safe and object oriented object that can point to another method or multiple methods which can then be invoked later.

Can we pass delegate as parameter?

You can pass methods as parameters to a delegate to allow the delegate to point to the method. Delegates are used to define callback methods and implement event handling, and they are declared using the “delegate” keyword. You can declare a delegate that can appear on its own or even nested inside a class.

What is the difference between action and delegate?

The only difference between Action Delegates and Function Delegates is that Action Delegates does not return anything i.e. having void return type. An Action Delegate can also be initialized using the new keyword. Action<int> val = new Action<int>(myfun);

How do you call a method with parameters in another method in C#?

You can use the Action delegate type. Then you can use it like this: void MyAction() { } ErrorDBConcurrency(e, MyAction); If you do need parameters you can use a lambda expression.


1 Answers

Declare the action delegate you need, there's nothing magic about it:

public delegate void Action<T1, T2, T3, T4, T5>(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5);
like image 131
Lasse V. Karlsen Avatar answered Sep 21 '22 22:09

Lasse V. Karlsen