Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of function pointers in C#

There is a set of methods like:

  Foo(int, float, params objects[])
  Goo(int, params objects[])
  Too()

each taking different number of & type of parameters (so are return values).

I read an integer (an index) from a database. The integer corresponds to one of the above method (1 for Foo, 2 for Goo and 3 for Too).

How do I store above methods (as delegates) in a collection so that I can call appropriate method by indexing into the collection using the integer read from db as index.

like image 344
Manjukarunakar Avatar asked Jul 11 '26 00:07

Manjukarunakar


1 Answers

You could just use a Delegate[] - when you read the integer, you'll then need to cast the relevant array value to an Action<int, float, object[]>, an Action<int, object[]> or an Action before you call it though.

like image 186
Jon Skeet Avatar answered Jul 13 '26 12:07

Jon Skeet



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!