delegate type parameters

We can use delegate as a type for method parameter.


using System;

delegate void Printer(int i);
class Test
{
    static void consolePrinter(int i)
    {
        Console.WriteLine(i);
    }

    static void output(int[] intArray, Printer p)
    {
        foreach (int i in intArray)
        {
            p(i);
        }
    }

    static void Main()
    {
        Printer p = consolePrinter;
        int[] intArray = new int[] { 1, 2, 3 };
        output(intArray, p);

    }
}

The output:


1
2
3
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.