C# code

Posted on 2011-04-25
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 85

    Expert Comment

    by:Mike Tomlinson
    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


    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now