add (C# Reference)
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.
Example
The following example shows an event that has 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 => PreDrawEvent += value;
remove => 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.
See also
Співпраця з нами на GitHub
Джерело цього вмісту можна знайти на GitHub, де також можна створювати й переглядати запитання та запити на внесення змін. Докладні відомості наведено в нашому посібнику для співавторів.