Return a delegate

In this chapter you will learn:

  1. Return delegate from a method

Return delegate from a method

using System;//from  j a v a2s .  c  o m

public delegate void MyDeleage();

public class MainClass
{
    public static MyDeleage[] CreateDelegates() {
        MyDeleage[] delegates = new MyDeleage[3];
        
        for( int i = 0; i < 3; ++i ) {
            delegates[i] = delegate {
                Console.WriteLine( "Hi" ); 
            };
        }
        return delegates;
    }

    static void Main() {
        MyDeleage[] delegates = CreateDelegates();
        for( int i = 0; i < 3; ++i ) {
            delegates[i]();
        }
    }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Use delegate as the function parameter
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