One can use the following construct for declaring an event:
public class MyClass
{
public event EventHandler<EventArgs> SomeEvent = (s,e) => {};
public void SomeMethod ()
{
// Do something interesting... ;)
SomeEvent (this, new EventArgs);
}
}
That allows raising the event without the need to check if the event is null.
Now, let's say that an object A holds a reference to an object of MyClass, registers for the event and then unregisters it later on.
var myClass = new MyClass();
myClass.SomeEvent += MyHandler;
...
myClass.SomeEvent -= MyHandler;
myClass = null;
Will the GC collect myClass even if there is a no-op lambda expression still on the event?
I guess so because the object root is no longer reference by other objects... Can anyone confirm or prove otherwise?