C# code

Posted on 2011-04-25
Medium Priority
Last Modified: 2012-05-11
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
                        this.dataChangedEvent += value;
                        this.dataChangedEvent -= value;

////DataUpdateEventHandler is:-->
public delegate void DataUpdateEventHandler(object command, DataUpdateEventArgs eventInfo);
////and  DataUpdateEventArgs is inheriting from EventArgs.
public class DataUpdateEventArgs : EventArgs
Question by:vivekj2004
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35460722
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.
LVL 10

Accepted Solution

Asim Nazir earned 2000 total points
ID: 35464692

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.


Nice discussion on the same:

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.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question