c# - Does using a no-op lambda expression for initializing an event prevent GC? -


one can use following construct declaring event:

public class myclass {     public event eventhandler<eventargs> someevent = (s,e) => {};      public void somemethod ()     {         // interesting... ;)         someevent (this, new eventargs);     } } 

that allows raising event without need check if event null.

now, let's object holds reference object of myclass, registers event , unregisters later on.

var myclass = new myclass(); myclass.someevent += myhandler; ... myclass.someevent -= myhandler; myclass = null; 

will gc collect myclass if there no-op lambda expression still on event?

i guess because object root no longer reference other objects... can confirm or prove otherwise?

with code in question gc collect myclass if don't unsubscribe. relation other way around. myclass's event holds reference subscriber theoretically should worried subscriber not being collected. if unsubscribe subscriber collected.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -