#391 – Passing a Delegate Instance as a Parameter

When you use the delegate keyword to define a delegate, you’re actually defining a new class, which inherits from System.MulticastDelegate.

        public delegate string StringProcessorDelegate(string input);

When you declare a variable of this delegate type and assign a method to it, you’re creating an instance of the new class.

            StringProcessorDelegate stringProcessor = ReverseAndHighLight;

 

        static string ReverseAndHighLight(string input)
        {
            // Reverse, add asterisks
        }

You now have a delegate instance that is set up to invoke the ReverseAndHighlight method.  This instance is actually just a normal .NET object, since it’s an instance of the new type that derives from System.MulticastDelegate.  This means that you can pass this object to a method, as a parameter.

            string result = DoStringProcessing(stringProcessor, "Basil Rathbone");

 

        static string DoStringProcessing(StringProcessorDelegate del, string input)
        {
            Console.WriteLine("Input string: {0}", input);
            string result = del(input);
            Console.WriteLine("Result: {0}", result);

            return result;
        }

Advertisement

#375 – Using GetInvocationList to Get a List of Individual Delegates

Delegates defined in C# using the delegate keyword are multicast delegates, meaning that they can hold zero or more references to methods.

In the example below, we define a delegate type, create a delegate instance and then point it to two methods.

        private delegate void StringHandlerDelegate(string s);

        static void Main()
        {
            // Add two methods to delegate instance
            StringHandlerDelegate del = Method1;
            del += Method2;
        }

At this point, if we invoked the delegate, both methods would be called.

You can use the GetInvocationList method of System.MulticastDelegate to get a list of individual delegates, each of which represents a single method.

            Delegate[] dels = del.GetInvocationList();

Notice that each element in the array, while only representing a single method to be called, is itself an instance of the original multicast delegate, i.e. StringHandlerDelegate.

#374 – A Custom Delegate Derives from System.MulticastDelegate

You define a new delegate in C# using the delegate keyword.

        private delegate void StringHandlerDelegate(string s);

This is equivalent to defining a new class that inherits from System.MulticastDelegate (which in turn inherits from System.Delegate).  MulticastDelegate is a class that knows how to refer to a set of methods–known as as the delegates invocation list.

Later in your code, you declare an instance of your delegate and set it equal to a method.

            StringHandlerDelegate del = Method1;

When you do this, you’re creating an instance of the new MulticastDelegate subclass and giving it a reference to the Method1 method.

You can also add a second method to the invocation list using the += operator.

            del += Method2;

At this point, a new delegate is created and System.Delegate.Combine method is then called to combine the two delegates into a new delegate that points to both methods.