#1,019 – Syntax for Adding Methods to a Delegate’s Invocation List

If you have a delegate instance, it will have zero or more methods in its invocation list.  When you invoke the delegate instance, these are the methods that will be called.

For a given delegate instance, you can add or remove methods to its invocation list.  In actuality, since a delegate is immutable, you’re actually creating a new delegate instance that has the specified methods added or removed.

Below are several examples of how you might add a new method to the invocation list of an existing delegate instance.

            // Add Method2 to original invocation list
            StringHandlerDelegate del1 = Method1;
            del1 += Method2;
            del1("Howdy 1");

            // Does the same thing
            del1 = Method1;
            del1 = del1 + Method2;
            del1("Howdy 2");

            // Does the same thing
            del1 = Method1;
            del1 = (StringHandlerDelegate)Delegate.Combine(del1, new StringHandlerDelegate(Method2));
            del1("Howdy 3");

1019-001

Advertisement