Jump to content

11 posts in this topic

Posted

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!

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

Ok, I will take a look. Thank you!

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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!

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

Thank you! I will be sure and have a look. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.