tags:

views:

172

answers:

2


I've got this example where I convert a C# 2 delegate example:

Predicate<string> predicate2 = delegate(string n)
{
    return n.StartsWith("J");
};
IList<string> namesWithJ2 = Tools.Filter(names, predicate2);
Tools.Dump(namesWithJ2);

to C# 3 lambda syntax example:

var filteredNames = Tools.Filter(names, n => n.StartsWith("J"));
Tools.Dump(filteredNames);


But how to I convert this to lambda syntax? Particularly, how do I get the two parameters (object s, DoWorkEventArgs args) to be passed using the "=>"?

_worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
    BackgroundWorker worker = s as BackgroundWorker;
    for (int i = 0; i < 10; i++)
    {
        if (worker.CancellationPending)
        {
            args.Cancel = true;
            return;
        }

        Thread.Sleep(1000);
        worker.ReportProgress(i + 1);
    }
};
+1  A: 
_worker.DoWork += (s, args) => {
    ....
};

Or if the compiler can't figure out the exact types of s and args:

_worker.DoWork += (object s, DoWorkEventArgs args) => {
    ....
};
Oren Trutner
+1  A: 

The outline of the form is

_worker.DoWork += (s, args) => {body of method};

Other punctuation as the compiler advises

Steve Gilham