Do lambdas assigned to an event prevent garbage collection of the owning object?

c#, events, garbage-collection, lambda

Solution

No, `o` will get freed, and so will the lambda function. There are no references to `o` from anywhere else, so there is no reason that it should not get freed.

Problem

Say you have a class with an event property. If you instantiate this class in a local context, without outside references, would assigning a lambda expression to the event prevent the instance from becoming garbage collected? ``` { var o = new MyClass(); o.MyClassEvent += (args) => {}; } // Will 'o' be eligible for garbage collection here? ```

Original source

Related problems