Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between lambda expression and method group

What's the difference between

Class1.Method1<Guid, BECustomer>("cId", Facade.Customers.GetSingle);

and

Class1.Method1<Guid, BECustomer>("cId", x => Facade.Customers.GetSingle(x));

?

Resharper suggests to use the first expression.

like image 595
Alexandre Avatar asked Jul 12 '11 10:07

Alexandre


People also ask

How do you convert lambda expression to method?

If you are using a lambda expression as an anonymous function but not doing anything with the argument passed, you can replace lambda expression with method reference. In the first two cases, the method reference is equivalent to lambda expression that supplies the parameters of the method e.g. System.

What is a method group?

A method group is the name for a set of methods (that might be just one) - i.e. in theory the ToString method may have multiple overloads (plus any extension methods): ToString() , ToString(string format) , etc - hence ToString by itself is a "method group".

What is lambda expression in LINQ?

The term 'Lambda expression' has derived its name from 'lambda' calculus which in turn is a mathematical notation applied for defining functions. Lambda expressions as a LINQ equation's executable part translate logic in a way at run time so it can pass on to the data source conveniently.

What are lambda expressions in C#?

A lambda expression with an expression on the right side of the => operator is called an expression lambda. An expression lambda returns the result of the expression and takes the following basic form: C# Copy. (input-parameters) => expression. The body of an expression lambda can consist of a method call.


3 Answers

There is no difference in regards to the result. However, the second one creates an additional redirection: The code will first call your anonymous method the takes one parameter named x and that in turn calls Facade.Customers.GetSingle with that parameter. This redirection has no benefit at all, that's why ReSharper tells you to use the first alternative.

like image 104
Daniel Hilgarth Avatar answered Oct 01 '22 00:10

Daniel Hilgarth


Behind the scenes, the compiler generates a lot more code if you use the lambda expression. With the method group, it just makes a new delegate pointing to that method:

L_0001: ldstr "cId"
L_0006: ldnull 
L_0007: ldftn void Facade/Customers::GetSingle(valuetype [mscorlib]System.Guid)
L_000d: newobj instance void [mscorlib]System.Action`1<valuetype [mscorlib]System.Guid>::.ctor(object, native int)
L_0012: call void Class1::Method1<valuetype [mscorlib]System.Guid, class BECustomer>(string, class [mscorlib]System.Action`1<!!0>)

With the lambda expression, an anonymous method is created on the class (<Test>b__0 on L_0025) and the delegate references that instead:

L_0018: ldstr "cId"
L_001d: ldsfld class [mscorlib]System.Action`1<valuetype [mscorlib]System.Guid> Class1::CS$<>9__CachedAnonymousMethodDelegate1
L_0022: brtrue.s L_0037
L_0024: ldnull 
L_0025: ldftn void Class1::<Test>b__0(valuetype [mscorlib]System.Guid)
L_002b: newobj instance void [mscorlib]System.Action`1<valuetype [mscorlib]System.Guid>::.ctor(object, native int)
L_0030: stsfld class [mscorlib]System.Action`1<valuetype [mscorlib]System.Guid> Class1::CS$<>9__CachedAnonymousMethodDelegate1
L_0035: br.s L_0037
L_0037: ldsfld class [mscorlib]System.Action`1<valuetype [mscorlib]System.Guid> Class1::CS$<>9__CachedAnonymousMethodDelegate1
L_003c: call void Class1::Method1<valuetype [mscorlib]System.Guid, class BECustomer>(string, class [mscorlib]System.Action`1<!!0>)
like image 21
dahlbyk Avatar answered Oct 05 '22 00:10

dahlbyk


Where your Method1<Guid, BECustomer> accepts a Func<Guid, BECustomer> argument, Func<Guid, BECustomer> is synonymous with:

public delegate BECustomer Func(Guid arg);

In fact, all a Func is is a generic delegate:

public delegate TResult Func<T, TResult>(T arg);

The compiler can analyse your code and determine that your Func<Guid, BECustomer> is compatible with the method group for Facade.Customers.GetSingle because the method signature matches the delegate signature.

This is syntactic sugar and is another example of the compiler doing the grunt work for you.

like image 1
Matthew Abbott Avatar answered Oct 02 '22 00:10

Matthew Abbott