C# Explicitly Removing Event Handlers

后端 未结 3 2119
后悔当初
后悔当初 2020-12-10 03:31

I was wondering if setting an object to null will clean up any eventhandlers that are attached to the objects events...

e.g.

Button button = new Butt         


        
3条回答
  •  我在风中等你
    2020-12-10 03:49

    If there are no other references to button anywhere, then there is no need to remove the event handler here to avoid a memory leak. Event handlers are one-way references, so removing them is only needed when the object with events is long-lived, and you want to avoid the handlers (i.e. objects with handler methods) from living longer than they should. In your example, this isn't the case.

提交回复
热议问题