delegate array

In this chapter you will learn:

  1. How to use delegate array

An array of delegate

using System;//from   j  av a2s. c  o  m

public delegate double ComputeDelegate( double x,double y );

public class MyClass
{
    public MyClass( ) {
    }

    public double Add( double x, double y ) {
        double result = x+y;
        Console.WriteLine( "InstanceResults: {0}", result );
        return result;
    }

    public static double Subtract( double x,double y ) {
        double result = x - y;
        Console.WriteLine( "StaticResult: {0}", result );
        return result;
    }

    private double factor;
}

public class MainClass
{
    static void Main() {
        MyClass proc1 = new MyClass( );
        MyClass proc2 = new MyClass( );

        ComputeDelegate[] delegates = new ComputeDelegate[] {
            new ComputeDelegate( proc1.Add ),
            new ComputeDelegate( proc2.Add ),
            new ComputeDelegate( MyClass.Subtract )
        };

        ComputeDelegate chained = (ComputeDelegate) Delegate.Combine( delegates );

        double combined = chained( 4, 5 );
        
        Console.WriteLine( "Output: {0}", combined );
    }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Return delegate from a method
Home » C# Tutorial » delegate, lambda, event
delegate
Multicast delegate
delegate variables
delegate parameters
Generic delegate
delegate return
Func and Action
delegate new
chained delegate
Anonymous delegate
delegate array
Return a delegate
delegate as parameter
Event
event multicast
static event
EventHandler
Event pattern
lambda
lambda syntax
Func, Action and lambda
lambda with outer function
lambda iteration variables