General .Net question
-
Is it enough to hold a delegate to an object method, in order to keep it being collected? And what about reference to some general member? Thanks, Yaakov
-
Is it enough to hold a delegate to an object method, in order to keep it being collected? And what about reference to some general member? Thanks, Yaakov
What exactly are you asking? Do you need to store a reference to a delegate in order to remove it from a event later? If so, the answer is "no". So long as the delegate references the same method for the same instance of a class that is already added to an event (which is a
MulticastDelegate
), you can "new" up another one like so:textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
// Later that day...
textBox1.TextChanged -= new EventHandler(textBox1_TextChanged);A delegate is like a managed function pointer. So long as the function pointer (the method to which the delegate points) is the same - the same method on the same instance or a static method for a class - the delegate is the same. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]