How does a lambda expression work? (CSharp)
I can't seem to wrap my mind around lambda expressions (in CSharp programming language). I've been told where to use them, and used them successfully, but I really have no idea what they do or how they work or what they're used for. Looking online, every explanation seems complicated and way more wordy (I guess) than it needs to be... perhaps a different use than this. There must be a simple explanation out there somewhere!
Here is an example piece of code using a lambda expression if anyone needs to see it. This was for an in-class lab, and though I wrote the other code, our teacher basically gave us that particular line (as he does any time we need a lambda), told us where it went, but I've never been given an explanation of it's use other than "it goes there".
http://i48.tinypic.com/29wwtue.jpg
Anyone?
Thanks!
You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.
M$2 Answers
It's not clear what it is that you really want to know, and what you already understand. It seems like you might want an explanation of one or more of the following...
- What is a lambda expression?
- Why would you use a lambda expression?
- How do you use lambda expressions in C#?
- How does C# implement lambda expressions?
If you can clarify what you already understand and what you don't, it'll probably help you get a good answer.
You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.
M$You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.
M$
This is quite good as an easy explanation...
-- Quote
Lambda expressions are simply functions/methods.
They have a different syntax, primarily so that they can be written in expression context (more on this shortly) instead of as a member of a class. However, that is all they are. For instance, the following lambda expression:
c => c + 1
is a function that takes one argument, c, and returns the value c + 1.
... Instead of writing a method, creating a delegate from the method, and passing the delegate ... as a parameter, you can simply write a lambda expression in-line as a parameter ...
-- /Quote
http://blogs.msdn.com/b/ericwhite/archive/2006/10/03/lambda-expressions.aspx
There is potentially a whole lot more to lambda expressions and the "lambda calculus", but you're not likely to come across it outside of functional programming.
I'm not sure just how much you can do with lambda expressions in C#, but in most normal programming languages they are just handy when you want to pass a function as a parameter to something, and it's not worth the bother of writing a whole function definition for something that is simple and only used once.
-- Quote
int source = new { 3, 8, 4, 6, 1, 7, 9, 2, 4, 8 };
foreach (int i in source.Where(x => x > 5))
Console.WriteLine(i);
-- /Quote
In this case the "Where" wants to be passed a function that it can use to decide which things to include and exclude.
The function that is to be passed is very simple, and not going to be used anywhere else. It's just a function that returns the value of x>5.
So it can be easily specified as a lambda expression, rather than going to the bother of defining a special greaterThanFive() function and passing that.
If you're going to use a style of programming where functions can be passed around as parameters to other functions, like with the Where method above, it's useful to also have an easy way to specify a simple function.
That simple way of specifying a function has the fancy name, "lambda expression", because there is something called "lambda calculus" which is a theory concerned with the nature of computable functions.
I hope this has demystified things at least a little!
The first 3 points, you've noted. I look at that underlined code and i have no idea what it does or why it's there, except I was told to put it there and it'll work. What it's working to do, I don't know. Don't know if that answers your question or not... As you can see, I'm pretty clueless!