We had been discussing on twitter+gtalk if lambdas are syntatic sugar or not. According to wikipedia, a syntatic sugar is a term coined by Peter J. Landin for additions to the syntax of a computer language that do not affect its functionality but make it "sweeter" for humans to use. Based on this description, what I see is that: We could build up an expression tree with a bunch of method calls before c# 3.0, but now we have an easier way of building it. It was even possible to build an expression tree runtime (take a look at db4o native queries and mono project). Moreover, lambdas aren’t just for expressions but also they can be converted into delegates.
Let’s do some demonstration now:
Action<MyClass> myAction=x=>x.Increment();
This piece of code is translated into following code
private static void Main(string[] args)
{
Action<MyClass> myAction = (CS$<>9__CachedAnonymousMethodDelegate1 != null) ? CS$<>9__CachedAnonymousMethodDelegate1 : (CS$<>9__CachedAnonymousMethodDelegate1 = new Action<MyClass>(Program.<Main>b__0));
}
(Please note that our lambda has been converted into another ordinary method at compile time)
This code could have been written using ordinary C# syntax.
As you know, Lambas can also be converted into an expression depending on the context:
Expression<Action<MyClass>> myExpression = x => x.Increment();
This one, for example, will be translated into
private static void Main(string[] args)
{
ParameterExpression CS$0$0000;
Expression<Action<MyClass>> myExpression = Expression.Lambda<Action<MyClass>>(Expression.Call(CS$0$0000 = Expression.Parameter(typeof(MyClass), "x"), (MethodInfo) methodof(MyClass.Increment), new Expression[0]), new ParameterExpression[] { CS$0$0000 });
}
Now instead of into method/delegate, lambda has been converted into an expression, which can be done using an existing C# syntax.
One thing to note, Lambda itself has nothing to do with expressions. They can be converted into Expressions and then it is a piece of code that make something with those expressions (means that expressions can be interpreted by some providers) but it is a _short cut_ of doing it.
After those examples, one of my friends told that if we are talking about IL/CLR syntatical sugars, then they would agree. We can also extend this statement into “any programming language other than assembly (or basically 101001000s) is a syntatical sugar.” I would have agreed if I hadn’t known the definition of programming language.
For me, the syntatical sugars of C# language are the followings:
- The using statement
- The var keyword
- Lambdas
- Anonymous methods
- The dynamic keyword
- Automatic properties
- Object/Collection initializers
On the other hand, Generics, for example, is not a syntatical sugar. It doesn’t fall into another code within the same language. I am not sure about if this definition is valid, but this is the way I think it.
Just my 2 ykrs, I am trying to draw the boundaries of a syntatical sugar, and this post, hopefuly, will serve this purpose.

11e74edf-8a8b-45ce-bc2e-52fe52fd88b4|0|.0
c#