#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");



About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: