Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array containing Methods

Tags:

arrays

methods

c#

I was wondering if you can create an Array or a List<> that contains methods. I don't want to use a switch or lots of if statements.

Thanks

like image 670
Ruben Hamers Avatar asked Oct 10 '11 11:10

Ruben Hamers


People also ask

How do I check if an array contains a value?

JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.

What is an array and its methods?

In JavaScript, an array is a data structure that contains list of elements which store multiple values in a single variable. The strength of JavaScript arrays lies in the array methods.

How do you check if an array contains a string?

You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.


1 Answers

There you go

List<Action> list = new List<Action>();
list.Add( () => ClassA.MethodX(paramM) );
list.Add( () => ClassB.MethodY(paramN, ...) );

foreach (Action a in list) {
    a.Invoke();
}
like image 120
Pete Houston Avatar answered Oct 13 '22 15:10

Pete Houston