remove (C# Reference)

 

Updated: July 20, 2015

System_CAPS_ICON_note.jpg Note

For the latest documentation on C#, visit the C# Guide on docs.microsoft.com.

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.

The following example shows an event with custom add and remove accessors. For the full example, see How to: Implement Interface Events.


    class Events : IDrawingObject
    {
        event EventHandler PreDrawEvent;

        event EventHandler IDrawingObject.OnDraw
        {
            add
            {
                lock (PreDrawEvent)
                {
                    PreDrawEvent += value;
                }
            }
            remove
            {
                lock (PreDrawEvent)
                {
                    PreDrawEvent -= value;
                }
            }
        }

    }

You do not typically need to provide your own custom event accessors. The accessors that are automatically generated by the compiler when you declare an event are sufficient for most scenarios.

Events

Show: