locked
attach & deattach delegate events RRS feed

  • Question

  • how do we attach & deattach delegate events using +,- operators plz explain it with example
    Tuesday, May 24, 2011 1:20 PM

Answers

  • Hi Rafaqat,

    Based on your description, I think the link Petro's provide can help you understand how do you attach and deatach delegate event using '+' or '-'. Do you still have any problem with that?

    To chech the link and check the code sample in the link. Such as the following code segment in the link:

     // Add "ListChanged" to the Changed event on "List".
         List.Changed += new ChangedEventHandler(ListChanged);
    
    // Detach the event and delete the list
         List.Changed -= new ChangedEventHandler(ListChanged);
    


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by rafaqat Wednesday, June 1, 2011 4:18 PM
    Wednesday, June 1, 2011 8:57 AM

All replies

  • Take a look at Events Tutorial (C#) at MSDN
    Please remember to mark the replies as answers if they help
    • Proposed as answer by Geert van Horrik Tuesday, May 24, 2011 6:56 PM
    • Unproposed as answer by rafaqat Thursday, May 26, 2011 5:04 PM
    Tuesday, May 24, 2011 1:29 PM
  • One thing you should think about is when using dynamic delegates. Then you will have to store the delegate in a variable, and inside the delegate execution unsubscribe. Therefore, it's better not to use dynamic delegates unless you really know what you are doing.
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a free open-source MVVM framework for WPF, Silverlight and Windows Phone 7? Check out Catel!
    Tuesday, May 24, 2011 6:58 PM
  • There are also few other points:

    • There is a pattern to prevent null check for events (actually I am avoiding using this pattern. One of its drawbacks is that actually empty delegate will be invoked, which might potentially result in performance penalty):
    		//Assign empty handler
    		public event EventHandler MyEvent= (s, e) => { };
    		
    		protected virtual void OnMyEvent(EventArgs args)
    		{
    		 // Null reference check is not needed
    		 MyEvent(this, args);
    		}
    
    • If an application operates in multi-threaded environment, thread safety for events should be taken into account. A good discussion about this might be found here: Threadsafe events.

    Please remember to mark the replies as answers if they help

    Wednesday, May 25, 2011 7:43 AM
  • One of its drawbacks is that actually empty delegate will be invoked, which might potentially result in performance penalty

    AFAIK, there is no such thing as an empty delegate.
    Wednesday, May 25, 2011 1:40 PM
  • Hi Rafaqat,

    Based on your description, I think the link Petro's provide can help you understand how do you attach and deatach delegate event using '+' or '-'. Do you still have any problem with that?

    To chech the link and check the code sample in the link. Such as the following code segment in the link:

     // Add "ListChanged" to the Changed event on "List".
         List.Changed += new ChangedEventHandler(ListChanged);
    
    // Detach the event and delete the list
         List.Changed -= new ChangedEventHandler(ListChanged);
    


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by rafaqat Wednesday, June 1, 2011 4:18 PM
    Wednesday, June 1, 2011 8:57 AM