Say you announced
public delegate void foo(int x); public static void foo1(int x) { } public static void foo2(int x) { } public static void foo3(int x) { }
Now you can combine them directly with Delegate.Combine , if you don't mind typing the delegate name twice:
foo multicast = (foo)Delegate.Combine(new foo[] { foo1, foo2, foo3 });
Or you can write a generic function to combine them if you don't mind typing the delegate name once:
public static T Combine<T>(params T[] del) where T : class { return (T)(object)Delegate.Combine((Delegate[])(object[])del); } foo multicast2 = Combine<foo>(foo1, foo2, foo3);
Or you can write a non-generic function to combine them, if you don't want to enter a delegate name at all:
public static foo Combine(params foo[] del) { return (foo)Delegate.Combine(del); } foo multicast3 = Combine(foo1, foo2, foo3);
source share