C# – Remove handlers on disposing object

cnet

I can think of a few messy ways to solve this, but it strikes me that there should be a far more elegant solution than those which I've already come up with.

What's the most appropriate way for an object to cleanse itself of all of its event handlers prior to being disposed. It's a shame the event handler can't be enumerated upon.

In theory, is it considered more correct for the code adding the handler to an object to remember to remove it than assuming the object will clean itself up before it goes out of scope?

Best Answer

In theory, is it considered more correct for the code adding the handler to an object to remember to remove it than assuming the object will clean itself up before it goes out of scope?

To the above question, I'll have to say yes. The basic theory about events is that the event firer shouldn't be responsible for managing its own handlers; whoever added the event should do cleanup.