Add delegates in C#


Delegates are a confusing topic. A delegate is a data type much like int or bool but a delegate represents a method instead of a simple piece of data. Specific delegates represent methods that have specific signatures. For example, a delegate might represent a method that takes two int parameters and returns a float result.

After a program defines a delegate type, it can declare variables of that type. It can then store a reference to a method in a delegate variable and later invoke the method by calling whatever method is in the variable.

As if that wasn’t confusing enough, you can add delegates. So what does it mean to add delegates? If you add two delegates, the result is a new delegate that executes the first followed by the second. You can think of a delegate as a sort of list of references to methods. If you add two delegates, the second one’s methods get added to the first one’s list. Later if you execute the combined delegate, both lists are executed.

This example uses the following code to demonstrate delegate addition.

// A delegate that represents a method
// that adds something to the ListBox.
private delegate void AddToListDelegate();

// Specific methods that match the delegate.
// Specific methods that match the delegate.
private void MethodA()
    lstResults.Items.Add("    This is MethodA");
private void MethodB()
    lstResults.Items.Add("    This is MethodB");

// Define and use three delegates.
private void Form1_Load(object sender, EventArgs e)
    // Define delegate variables.
    AddToListDelegate A = FunctionA;
    AddToListDelegate B = FunctionB;
    AddToListDelegate C = A + B;

    // Use the delegates.
    lstResults.Items.Add("Calling A:");
    lstResults.Items.Add("Calling B:");
    lstResults.Items.Add("Calling C:");
    lstResults.Items.Add("Calling C - A:");
    (C - A)();
    lstResults.Items.Add("Calling C -= A:");
    C -= A;

The code first declares the type AddTolistDelegate. A variable with this delegate type can hold a reference to a method that takes no parameters and has type void (it returns nothing).

Next the code defines two methods that match the delegate type.

The form’s Load event handler defines two delegate variables A and B, and initializes them so they refer to MethodA and MethodB. It then creates a third delegate C and sets it equal to A + B.

The code then executes the delegates. It calls the methods referred to by A, then B, and then C. The delegate C contains references to A and B so invoking it makes both MethodA and MethodB execute.

[Mastering Events and Delegates in C#]

Next the code subtracts A from C. That removes the call to A‘s method from the “list” contained in C leaving only B. When the code invokes the result, only MethodB executes.

Finally the code demonstrates the -= syntax to remove A from C. It executes C again to show that only B remains.

Note that you can subtract a delegate more than once from another delegate without harm. In this example, the code could remove A from C many times without causing any problems.

However, if you subtract the last delegate from another delegate, then the result is null and you can no longer invoke it. In this example if the code executed the statement C -= B, then C would become null and trying to invoke C would throw an exception.

For more information on delegates, see my O’Reilly video Mastering Events and Delegates in C#.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in syntax and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *