girlieq3000's Avatar
girlieq3000 10
112 Asked
761 Answered
455 Best
6
No one has voted on this question yet :(
1 year, 11 months ago

How does a lambda expression work? (CSharp)

(NOTE: Doing some more searching, I'm not even sure I have the right name for it. Perhaps this is something else entirely, and that's why I can't find it? See code sample below)

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!
Tip for best answer: M$0.81
Separate topics with commas, or by pressing return. Use the delete or backspace key to edit or remove existing topics.

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$

What is Your Answer?

0
0
0

2 Answers

2
philipy's Avatar
philipy | 1 year, 11 months ago
8
This isn't really going to be an answer to your question... more of a comment on it.... :)

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$
philipy's Avatar
philipy | 1 year, 11 months ago Report

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!

girlieq3000's Avatar
girlieq3000 | 1 year, 11 months ago Report

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!

Report Abuse

Post Reply Cancel
0
bieberhood's Avatar
bieberhood | 1 year, 9 months ago
1
A lambda expression is like an inline delegate. The argument for methods that accept an expression can be written like this: Func expression where Customer is the type of argument and bool is the return type. So in the example you have underlined: the "c=>" part is telling the FirstOrDefault extension method that you are going to use the name "c" to represent the argument passed to the function and the "c.CustomerID == cid" represents the boolean value that will be returned. for a generic list of customers or a List the underlined statement is similar to doing this var cust = myList.Find(delegate(Customer c){return c.CustomerID == cid}); Expressions are extremely powerful when you use them with generics. You can build very useful classes that can extend other classes or be used as utilities. To further your understanding of these expressions and how they are actually working, I recommend you use http://www.red-gate.com/products/reflector/ to take a look at the System.Linq.Extensions class and it's methods. There is a wealth of information in there.
source(s):
I've been a .NET Developer since 2007
http://www.red-gate.com/products/reflector/

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$

Report Abuse

Post Reply Cancel

Learn something new with our FREE educational apps!

Private lessons in the comfort of your own home. Get back in shape or finally pick up a guitar with our great experts guiding you the whole way!
Learn Guitar
Learn Hip Hop
Learn Pilates