tags:

views:

516

answers:

4

I am an experienced developer in C# and working on LOB application from last 7 Years, I have some issues in understanding the usage of Lambda expression in programming.

As far as I understand, it is useful in case of

  1. Working with LINQ (Grouping, Select,Where etc..)
  2. We can pass Lambda expression to any function as argument, so it can be used in place of delegate or anonymous function or normal function.
  3. We can create generic lambda function which takes any datatype variable as argument and can return any datatype, e.g.

    MyFirstLambdaFunc((val1,val2) => val1+val2)
    public R MyFirstLambdaFunc(Func lambdaexpression,T x,T y)
    {
      R Result = lambdaexpression(x, y);
      return Result;
    }
    
  4. Coding can be compact

Now the question is:

  1. Are there any other advantages?
  2. When we pass lambda expression as function, can we pass only a single line operation?
  3. Can anybody have some case study or some practical example document?

Thanks in Advance

Harish Bhattbhatt

+2  A: 

For non-expression trees, you can certainly write multi-line lambda expressions. Just this morning I wrote something along the lines of:

 var query = foo.Select(x => {
               string text = x.Value;
               int delimitedIndex = text.IndexOf(' ');
               return int.Parse(text.Substring(0, delimitedIndex);
             });

Lambda expressions can be useful almost anywhere that you want to create delegate instances with reasonably simple implementations.

To provide another case in point, suppose you want to start some threads to do work on each of many values. (Let's ignore the fact that we're creating lots of threads - the same technique works for threadpools too.) You can use something like:

foreach (string url in urls)
{
    // Slight quirk due to variable capture semantics
    string urlCopy = url;
    new Thread(() => FetchUrl(urlCopy)).Start();
}

Doing that in a strongly-typed way normally is tricky - there's ParameterizedThreadStart, but that's not generic, so it becomes fiddly. This is pretty clean IMO.

Jon Skeet
I would say that lambda expressions are a very good "closure" tool. Don't you agree? Sometimes I neeid to write a simple function witch needs to know "a lot of state" from t's ancestor so it would take a lot of parameters. Lambda expressions help in these situations...
bruno conde
A: 

Thanks Jon,

Your Multi line function is very well received

x => { string text = x.Value; int delimitedIndex = text.IndexOf(' '); return int.Parse(text.Substring(0, delimitedIndex); }

if possible can you provide some more examples or situation where lambda expression can be of great help in place of other programming techniques...only if possible

I think this will help so many people because knowing things technically is not sufficient but where to use it practically and how it will help in particular situation is very important.

Harryboy
The things posted down here are answers. To ask a follow-up question, you can leave comments (not answers), or edit your question, or create a whole new question. As for examples of lambdas, try searching this site - there are thousands of examples.
Daniel Earwicker
Surely...as i was new to the site, i was not aware at that point of time...sorry from my side
Harryboy
A: 

I've used lambdas quite a bit as event handlers, e.g:

button.Click += (sender, e) => DoSomething();

I feel that it creates a very clean syntax and occasionally the closure semantics can be useful for handling events.

Gibsnag
A: 

The new Task Parallel Library of .NET 4 uses it extensively. For example, if you want to run a block of code in parallel, you can write:

 Parallel.For(1, 10, i =>
 {
   // do work
 });

This works much nicer than just passing a method to it in my opinion, like this:

 Parallel.For(1, 10, SomeMethodWithIntAsParam);
JulianR