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.
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.
GetSingle
method, whereas the second one passes the "function pointer" of the anonymous method. –
Bicolor Facade.Customers
changes as time goes by. Then the first one (method group from named method) will keep the original Customers
, whereas the second one (anonymous function from lambda) will reflect the new Customers
(Customers
is re-evaluated by the anonymous function). So in the second one, evaluation is "deferred". If this is hard to understand, try out this code: string test = "12345"; Func<string, bool> f = test.Contains; Func<string, bool> g = x => test.Contains(x); test = "changed!"; bool a = f("34"); bool b = g("34");
. –
Evetteevey Facade.Customers
looked pretty static to me. I explained the difference in more detail in this answer. Thanks. –
Bicolor ref
parameter. Namely the code static void M1(ref string s) { Func<string, bool> f1 = s.Contains; /* OK */ }
is fine; however the code static void M2(ref string s) { Func<string, bool> f2 = x => s.Contains(x); /* Compile-time error, cannot close over 'ref' parameter */ }
is illegal. –
Evetteevey int? a = 42; Func<int> f1 = a.GetValueOrDefault;/* interestingly not allowed */ Func<int> f2 = () => a.GetValueOrDefault();/* fine of course */
. Even when both are allowed (for methods inherited by Nullable<>
from its base classes), they may behave differently when the "target" HasValue
is false. Example: int? a = null; Func<string> g = a.ToString;
–
Evetteevey 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>)
Method1
for each item in a large set - a new instance of the anonymous class would be created on each iteration. That said, it's unlikely using a lambda instead of method group will measurably impact your app. Only measuring can tell you for sure though. –
Variometer 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.
© 2022 - 2024 — McMap. All rights reserved.