Why are lambdas convertible to expressions but method groups are not?

c#, c#-4.0, lambda

Solution

Because, in order to get the expression tree, we need a representation of the method in (uncompiled) source form. Lambda expressions are locally available in the source code and therefore are always available uncompiled. But methods may not be from inside the current assembly, and may thus be available only in compiled form.

Granted, the C# compiler could decompile the assembly’s IL code to retrieve an expression tree but as you mentioned, implementing feature costs money, this particular feature isn’t trivial, and the benefits are unclear.

Problem

LINQPad example: ``` void Main() { One(i => PrintInteger(i)); One(PrintInteger); Two(i => PrintInteger(i)); // Two(PrintInteger); - won't compile } static void One(Action<int> a) { a(1); } static void Two(Expression<Action<int>> e) { e.Compile()(2); } static void PrintInteger(int i) { Console.WriteLine(i); } ``` Uncommenting the `Two(PrintInteger);` line results in an error: cannot convert from 'method group' to 'System.Linq.Expressions.Expression<System.Action<int>>' This is similar to Convert Method Group to Expression, but I'm interested in the "why." I understand that Features cost money, time and effort; I'm wondering if there's a more interesting explanation.

Original source

Related problems