8

I'm beginning to program in C# 2.0, so I have never used lambda expressions, but, why so much fuss about it? Are them just syntactic sugar around anonymous delegates, or is there something more which I can't see?

3
  • Novelty is its own reward: arstechnica.com/old/content/2008/06/… Commented Jul 20, 2009 at 17:38
  • 1
    Yeah, it's a duplicate, "what's the purpose of lambdas" has been asked a zillion times. But I don't think people seem to take the idea of duplicates at all seriously anymore. Commented Jul 20, 2009 at 17:39
  • 14
    Yeah, it's a duplicate, "what's the purpose of lambdas" has been asked a zillion times. But I don't think people seem to take the idea of duplicates at all seriously anymore. Commented Jul 20, 2009 at 17:58

4 Answers 4

26

Well, lambda expressions have two main things over anonymous methods:

  • They're more concise than anonymous methods
  • They can be converted to expression trees as well as delegates

Unless you're using expression trees, they're extremely similar to anonymous methods though. The difference is that often you can write several lambda expressions in one statement (chaining method calls together) without losing readability, but anonymous methods are just a bit too wordy.

By the way, it's not so much that lambda expressions are "just syntactic sugar around anonymous delegates" as that both lambda expressions and anonymous methods are "just syntactic sugar around creating delegates (and expression trees)."

Don't discount syntactic sugar though - the benefits of anonymous functions acting as closures is massive, along with the ability to have the code right where you want it, instead of in a separate method.

Sign up to request clarification or add additional context in comments.

5 Comments

JABTW is my new abbreviation for the day - just a bit too wordy. Perfect for code reviews.
+1 Mmmmmm...expression trees.
So are expression used so aggregates of anonymous methods can be collapsed for code that performs essentially as well as manually written methods?
@280Z28 - I don't really know what you mean, I'm afraid.
Thanks a lot you all, guys. Seems that I have to read a lot of things to begin understanding this.
14

They can easily be used as just syntax sugar around a delegate but the big thing about lambdas is that the compiler has the ability turn them into expression trees which open up many possibilities (not the least of which being LINQ).

2 Comments

While that's the big thing in terms of LINQ to SQL etc, I personally use the fact that they're terser more often than I use expression trees...
Me as well - but I think that without expression trees and LINQ the C# team would have been less inclined to add lambda expressions to the language for terseness sake alone :)
4

Having a very terse syntax makes it more likely that more things will be built around them. Imagine a complex Linq query without any kind of syntactic sugar.

1 Comment

I fear I don't do Linq. I'm stuck in c# 2.0 for now, So I can't imagine that. Thanks anyway.
4

Are them just syntactic sugar around anonymous delegates, or is there something more which I can't see?

Good question. The answer is complicated. First off, obviously expression trees are the big one. But there are some subtleties as well. Here are my five prolix and frequently digressing articles on the subject of how lambdas are subtly different from anonymous methods:

http://blogs.msdn.com/ericlippert/archive/2007/01/10/lambda-expressions-vs-anonymous-methods-part-one.aspx

http://blogs.msdn.com/ericlippert/archive/2007/01/11/lambda-expressions-vs-anonymous-methods-part-two.aspx

http://blogs.msdn.com/ericlippert/archive/2007/01/12/lambda-expressions-vs-anonymous-methods-part-three.aspx

http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx

http://blogs.msdn.com/ericlippert/archive/2007/03/28/lambda-expressions-vs-anonymous-methods-part-five.aspx

All my articles about issues involving lambda expressions are archived here:

http://blogs.msdn.com/ericlippert/archive/tags/Lambda+Expressions/default.aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.