Links

Categories

Tags


« | Main | »

Anonymous delegates

By Jewe | October 29, 2012

import stdlib;
using stdlib;

// define a delegate type for our ForEach() function
delegate Printer(string item);

// function that calls a delegate for every element of an array
function ForEach(string[] values, Printer func)
{
    for (int i = 0; i < values.length; i++)
    {
        func(values[i]);
    }
}

function string main(const string[] args)
{
    // create an array of strings
    string[] names = { "Judy", "Christopher", "Helen", "James", "Sandra", "Rick", "Elisabeth" };

    // call anonymous function for every item in the array
    ForEach(names, function { println(item); } );

    return null;
}

Topics: code examples | Comments Off on Anonymous delegates

Comments are closed.