2

I usually get code samples that uses lambda expressions. I am stil using .net 2.0, and find it difficult to work with such code, for example

foreach(var item in items)
{
    var catCopy = item;
    foreach(var word in words)
    {
        var wordCopy = word;
        var waitCallback = new WaitCallback(state =>
        {
            DoSomething(wordCopy, catCopy);
        });

        ThreadPool.QueueUserWorkItem(waitCallback);
    }
}

how do i convert such expression to any of its alternative(i.e non lambda code or anonymous methods)?

thanks

Smith
  • 5,765
  • 17
  • 102
  • 161
  • 2
    I would suggest taking some time and learn lambdas rather than converting code. – Magnus Jun 26 '11 at 09:55
  • @Magnus, it won't work in .net 2.0 or will it? – Smith Jun 26 '11 at 09:58
  • 1
    Do you work with .Net as developer or do the apps have to be deployed there? It's important, because Lambda and any other form of anonymous methods is compiler specific, whilst Linq is .Net 3.5 and up. You can use the 4.0 compiler to target 2.0, which means you can use Lambda's. This piece of code doesn't contain any Linq. – Dykam Jun 26 '11 at 10:05
  • In this case I would add `wordCopy` and `catCopy` to a Pair object (since tuple does not exists) and use that as the `state` object to the callback function. – Magnus Jun 26 '11 at 10:06
  • @Dykam i develop targeting .net 2 in some cases either by request, or mostly to avoid bordering the consumer with slow internet connection with large download. How can i use .net 4 compiler to target .net 2 and still use lambda? – Smith Jun 26 '11 at 14:24
  • Go to the project settings by rightclicking on the project -> properties. There is a setting to set the target framework. I think there is one to set the target compiler as well, but that defaults to 4.0. This is only when you use Visual Studio 10. – Dykam Jun 26 '11 at 17:05
  • @Dykam am using vs2008, i still need to know how to target .n2, but work with .net 4! – Smith Jun 26 '11 at 18:04
  • Well, then it is impossible, as the feature of the compiler being able to target multiple frameworks is since VS2010/.Net4.0/C#4 – Dykam Jun 26 '11 at 18:25

2 Answers2

6

A lambda expression in C# is really just a delegate. Given your using .Net 2.0 you can use anonymous methods to define a delegate on the fly, so replace line of code with:

var waitCallback = new WaitCallback(
                        delegate(object state) { 
                             DoSomething(workCopy, catCopy); 
                        });
balexandre
  • 73,608
  • 45
  • 233
  • 342
Swanny
  • 2,388
  • 17
  • 20
1

Why are you still using .Net 2? You're missing a lot of great changes, especially LINQ.

That being said, lambdas are not a feature of .Net 3.5, they are a feature of C# 3.0 and you can use that while compiling for .Net 2.0, if you really need to do that.

svick
  • 236,525
  • 50
  • 385
  • 514
  • how can i develop in .net 2 and use lambda, is there any reference? – Smith Jun 26 '11 at 14:22
  • @Smith, you have to use Visual Studio that supports it (2008 or 2010) and select target framework in the project properties. See [MSDN](http://msdn.microsoft.com/en-us/library/bb398202.aspx). – svick Jun 26 '11 at 14:26