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