Csharp/CSharp Tutorial/delegate/Return delegate

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Return delegate from a method

using System;
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]();
        }
    }
}
Hi
Hi
Hi