What does the phrase "delegate is cached" mean in this context (and in general what a cached delegate is and what it is for):
"In case of calling SomeMethod(OtherMethod) - a delegate will always be created. In case of calling SomeMethod(x => OtherMethod(x)) - the delegate will be cached."?
Context from here
UPD
Between the first call and the second there is a difference in MSIL 'e, namely this code:
static void SomeMethod(Func<int, int> otherMethod) { otherMethod(1); } static int OtherMethod(int x) { return x; } static void Main(string[] args) { SomeMethod(OtherMethod); // 1 SomeMethod(x => OtherMethod(x)); // 2 SomeMethod(x => OtherMethod(x)); // 3 } Will be converted to approximately the following:
static void Main() { SomeMethod(new Func<int, int>(OtherMethod)); if (C.foo != null) SomeMethod(C.foo) else { C.foo = new Func<int, int>(c, Cb) SomeMethod(C.foo); } if (C.foo1 != null) SomeMethod(C.foo1) else { C.foo1 = new Func<int, int>(c, C.b1) SomeMethod(C.foo1); } } [CompileGenerated] class C { public static C c; public static Func<int, int> foo; public static Func<int, int> foo1; static C() { c = new C(); } C(){} public int b(int x) { return OtherMethod(x); } public int b1(int x) { return OtherMethod(x); } } But as you can see, the compiler for 3 calls created and initialized a new "cached" variable, and did not use the old
SomeMethod(x => OtherMethod(x))not insideMain, but inside another function. For example,Wrapper, then with several calls to this function, the cached version should be used - Grundy