C# code

Can someone tell me the meaning of add and remove here? I know that there are getter and setter properties, but what does these mean (is it a property?)?

public virtual event DataUpdateEventHandler DataChanged
            {
                  add
                  {
                        this.dataChangedEvent += value;
                        issueFiredEvents(value);
                  }
                  remove
                  {
                        this.dataChangedEvent -= value;
                  }
            }

-----------------------
////DataUpdateEventHandler is:-->
public delegate void DataUpdateEventHandler(object command, DataUpdateEventArgs eventInfo);
--------------------------
////and  DataUpdateEventArgs is inheriting from EventArgs.
public class DataUpdateEventArgs : EventArgs
vivekj2004Asked:
Who is Participating?
 
Asim NazirConnect With a Mentor Commented:
Hi,

Yes. You can say, they are properties but specific to Events implementation. Instead of Get and Set, for events C# has Add and Remove accessors.

As per MSDN:
The add contextual keyword is used to define a custom event accessor that is invoked when client code subscribes to your event. If you supply a custom add accessor, you must also supply a remove accessor.

The remove contextual keyword is used to define a custom event accessor that is invoked when client code unsubscribes from your event. If you supply a custom remove accessor, you must also supply an add accessor.

http://msdn.microsoft.com/en-us/library/cc713642.aspx
http://msdn.microsoft.com/en-us/library/cc713648.aspx

Nice discussion on the same:
http://bytes.com/topic/c-sharp/answers/813497-property-accessors-add-remove-event-properties-andeventhandlerlist

Using += you can added event to handler while using -= you are removing it. So if  you debug, you will get into EventHandling method soon after Event handler is assigned using += but it will not if it's removed i.e. using -=.

I hope it helps.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The add/remove properties get fired when someone subscribes/unsubscribes from that event.  This allows you to take additional custom actions, such as running that issueFiedEvents() method.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.