Search code examples
c#garbage-collection

instance vs. method as argument -- understanding lifetime and garbage collection


In a contrived example, I have an instance supporting the following interface:

public interface ISelector<L, R>
{
    R Select(L value);
}

Now, I consider 2 ways of calling IEnumerable.Select<L, R>(Func<L, R>):

IEnumerable<L> l;
ISelector<L, R> selector;
IEnumerable<R> r;
r = l.Select(v => selector.Select(v)); // 1
r = l.Select(selector.Select); // 2

Way 1 captures the reference to the selector from the variable into a closure, and this closure will hold this reference independently of the scope of the variable selector. So the variable selector may go out of scope and r.ToList() may be called after variable selector went out of scope.

However, I am unsure of how to understand way 2: How is ISelector<L, R>.Select assigned and captured? And may the variable selector go out of scope and may r.ToList() be called after variable selector went out of scope?

Readability-wise I prefer way 2, yet before using it, I would like to understand the lifetime of the captured and garbage-collected parts first.


Solution

  • In the second example, selector.Select is interpreted as shorthand for a new delegate instance where selector is the target and ISelector<L, R>.Select is the MethodInfo. As such, selector is reachable via the delegate instance, so: as long as the delegate is reachable, the object of selector is reachable, and cannot be collected. So yes, you can call ToList() safely at any time.

    Specifically, here, we see for var projected l.Select(obj.Select);

    // l. [push 1]
    IL_000d: ldloc.1 
    // obj. [push 1]
    IL_000e: ldloc.0 
    // Select [push 1]
    IL_000f: ldftn instance string X::Select(int32) 
    // new Func<int,string>([pop 2, push 1])
    IL_0015: newobj instance void class [System.Private.CoreLib]System.Func`2<int32, string>::.ctor(object, native int)
    // Enumerable.Select<int,string>([pop 2, push 1])
    IL_001a: call class [System.Private.CoreLib]System.Collections.Generic.IEnumerable`1<!!1> [System.Linq]System.Linq.Enumerable::Select<int32, string>(class [System.Private.CoreLib]System.Collections.Generic.IEnumerable`1<!!0>, class [System.Private.CoreLib]System.Func`2<!!0, !!1>)
    // projected=[pop 1]
    IL_001f: stloc.2
    
    

    which is the delegate