Harish Bhattbhatt
Harish Bhattbhatt

Reputation:

Usage Of Lambda Expression in Practical Programming

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

Upvotes: 1

Views: 2873

Answers (4)

JulianR
JulianR

Reputation: 16513

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); 

Upvotes: 0

Gibsnag
Gibsnag

Reputation: 1087

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.

Upvotes: 0

Harishbb
Harishbb

Reputation: 1114

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

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.

Upvotes: 2

Related Questions