I am experimenting with calling delegate functions from a delegate array. I’ve been able to create the array of delegates, but how do I call the delegate?
public delegate void pd(); public static class MyClass { static void p1() { //... } static void p2 () { //... } //... static pd[] delegates = new pd[] { new pd( MyClass.p1 ), new pd( MyClass.p2) /* ... */ }; } public class MainClass { static void Main() { // Call pd[0] // Call pd[1] } }
EDIT: The reason for the array is that I need to call the delegate functions by an index as needed. They are not run in response to an event. I see a critical (stupid) error in my code as I had tried to execute the delegate function using the pd[] type rather than the name of the array (delegates).
If they’re all the same type, why not just combine them into a single multicast delegate?