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?
-
Novelty is its own reward: http://arstechnica.com/old/content/2008/06/mri-study-suggests-novelty-is-its-own-reward.ars – Greg Jul 20 '09 at 17:38
-
1Yeah, 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. – Daniel Earwicker Jul 20 '09 at 17:39
-
14Yeah, 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. – Steven Sudit Jul 20 '09 at 17:58
4 Answers
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.

- 1,421,763
- 867
- 9,128
- 9,194
-
JABTW is my new abbreviation for the day - just a bit too wordy. Perfect for code reviews. – Jeff Yates Jul 20 '09 at 17:27
-
-
So are expression used so aggregates of anonymous methods can be collapsed for code that performs essentially as well as manually written methods? – Sam Harwell Jul 20 '09 at 17:39
-
-
Thanks a lot you all, guys. Seems that I have to read a lot of things to begin understanding this. – raven Jul 21 '09 at 20:40
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).

- 344,730
- 71
- 640
- 635
-
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... – Jon Skeet Jul 20 '09 at 17:33
-
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 :) – Andrew Hare Jul 20 '09 at 17:53
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.

- 87,846
- 14
- 132
- 192
-
I fear I don't do Linq. I'm stuck in c# 2.0 for now, So I can't imagine that. Thanks anyway. – raven Jul 21 '09 at 20:41
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:
All my articles about issues involving lambda expressions are archived here:
http://blogs.msdn.com/ericlippert/archive/tags/Lambda+Expressions/default.aspx

- 647,829
- 179
- 1,238
- 2,067