The following construction can be used to declare an event:
public class MyClass
{
public event EventHandler<EventArgs> SomeEvent = (s,e) => {};
public void SomeMethod ()
{
SomeEvent (this, new EventArgs);
}
}
This allows you to raise an event without having to check if the event is null.
Now let's say that object A contains a reference to the MyClass object, registers the event, and then cancels it later.
var myClass = new MyClass();
myClass.SomeEvent += MyHandler;
...
myClass.SomeEvent -= MyHandler;
myClass = null;
Will the GC compile myClass even if there is a no-op lambda expression in the event?
I think so, because the root of the object no longer refers to other objects ... Can anyone confirm or prove otherwise?
source
share