Joel asked: Can your language do this?

And he gave this example to illustrate his point:

function Cook( i1, i2, f )
    alert("get the " + i1);

Cook( "lobster",
    function(x) { alert("put " + x + " in pot"); } );
Cook( "chicken",
    function(x) { alert("boom boom the " + x); } );

Which outputs something like:

get the lobster
put lobster in pot
put water in pot

get the chicken
boom boom chicken
boom boom coconut.

Simple, yeh?

Now how would you go about doing this in C#?

I can't find a way to do it without defining a delegate first. Albeit, a generic delegate... but it's still not quite as 'elegant'.

private delegate void D<T>(T x);

Set up your cook function -- almost the same (except we use generic types, to mimic javascript's non-static typing.

private void Cook<T1>(T1 i1, T1 i2, D<T1>  f)


    MessageBox.Show("get the " + i1);




And then rather than passing in anonymous functions, we can pass in anonymous delegates...



   delegate(string x) { MessageBox.Show("put " + x + " in pot"); });




    delegate(string x) { MessageBox.Show("boom boom the " + x); });

Your turn. ;-)


My book "Choose Your First Product" is available now.

It gives you 4 easy steps to find and validate a humble product idea.

Learn more.

(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.