Add delegates to combine lists of methods 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 delegate types 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. For an example of using delegates, see the example Use delegates to pass a method's address to another method in C#.

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 sort of think of a delegate as a 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.
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:");
    A();
    lstResults.Items.Add("Calling B:");
    B();
    lstResults.Items.Add("Calling C:");
    C();
    lstResults.Items.Add("Calling C - A:");
    (C - A)();
    lstResults.Items.Add("Calling C -= A:");
    C -= A;
    C();
}

The code first declares the type AddTolistDelegate. A delegate of this 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 method 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.

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 that result. 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.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.