tags:

views:

633

answers:

4

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?

+14  A: 

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).

Andrew Hare
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
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
+4  A: 

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.

Lou Franco
I fear I don't do Linq. I'm stuck in c# 2.0 for now, So I can't imagine that. Thanks anyway.
Jaime Pardos
+24  A: 

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.

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

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

Eric Lippert