Search code examples
asp.neteventsweb-user-controls

do i need to unregister events in asp.net


I have a web user control that contains several other (web user) controls and subscribes to events the children raise.

I saw someone somewhere in a similar situation providing Dispose() on the containing control and unregistering the events it had subscribed to.

Isn't it all going to be blown away when the request processing completes? / Is unregistering those events necessary?


Solution

  • Since the contained controls (event publishers) will no longer be referenced by anything when the containing control itself is no longer referenced, they should be garbage collected. When that happens the containing control can also be garbage collected. Since these objects are all in the same generation, I don't see how the container could be collected any sooner. The unregistering of the event handlers doesn't seem to be necessary. If the event publishers were longer lived objects than the container, then it would make some sense.

    For more information, reference this discussion.