Does lambda use a no-op expression to trigger an event, prevents GC?

The following construction can be used to declare an event:

public class MyClass
{
    public event EventHandler<EventArgs> SomeEvent = (s,e) => {};

    public void SomeMethod ()
    {
        // Do something interesting... ;)
        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?

+2
source share
2 answers

GC myClass, . . MyClass , , . , .

+3

MyClass , "" .

"" , (MyClass ) . . -op , , .

+6

All Articles