• 0

Can someone explain where Lambda Expressions would be useful?


Question

I was wondering what the benefits of using Lambda Expressions are. Can someone think of examples of where they would be beneficial to use? Let's say that I have a website where I want to add and manipulate customer and product information from a database. Is that a good place to use them for instance? Thank you in advance!

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Lambdas are more than inline functions, they are also closures. Any lambda that captures local variables, fields, etc, is compiled not to a method but to a type. C++ programmers are familiar with this concept under the name of functors; it was the only way to do closures before the advent of lambda expressions in C++11. C# had support for closures before lambdas actually, in C# 2.0 there were already anonymous methods which are basically lambdas with an ugly syntax. With lambdas, LINQ and the Func and Action types, C# 3.0 made functional programming much easier.

Link to comment
Share on other sites

  • 0

Lambda expressions are good for one off items that require a function but not necessarily need a full blown function created.

 

One use could be finding an object within a list that contains a certain value for one of it's member attributes.

Link to comment
Share on other sites

  • 0

Ok, so it would be good for searching for information / generating a report on one specific product basically?

 

Just like for anonymous functions it's just to avoid flooding classes with methods with the most basic functionalities.

Link to comment
Share on other sites

  • 0

Oh ok! That makes it a lot clearer! I can definitely see now how using Lambdas would be useful instead of having tons of small methods all over the place. That is exactly what I was looking for (the reason they were created). Thank you!

Link to comment
Share on other sites

  • 0

Oh ok, so they can function as classes as well then?

In the very limited sense that they can capture local state (fields, local variables, etc), yes. I suggest you look at the Jon Skeet article I linked to get a better understanding of closures.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.