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.
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.
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:
With the lambda expression, an anonymous method is created on the class (
<Test>b__0
on L_0025) and the delegate references that instead:Where your
Method1<Guid, BECustomer>
accepts aFunc<Guid, BECustomer>
argument,Func<Guid, BECustomer>
is synonymous with:In fact, all a
Func
is is a generic delegate:The compiler can analyse your code and determine that your
Func<Guid, BECustomer>
is compatible with the method group forFacade.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.
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 callsFacade.Customers.GetSingle
with that parameter. This redirection has no benefit at all, that's why ReSharper tells you to use the first alternative.