C#idiomatic C#
addcsharp/language-reference/keywords

Language

The `add` contextual keyword (C# Reference)

The `add` contextual keyword declares an event accessor that adds a handler to that event.

  • page csharp/language-reference/keywords/add
  • updated 01/21/2026

Use the add contextual keyword to define a custom event accessor that's invoked when client code subscribes to your event. If you supply a custom add accessor, you must also supply a remove accessor.


ms.custom: "updateeachrelease"

The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.

The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.

The following example shows an event that has custom add and remove accessors. For the full example, see How to implement interface events.

sample · csharpdocs/csharp/language-reference/keywords/snippets/events.csAddHandler

csharp
namespace Events;   
   
//<EventExample>
public class SampleEventArgs
{
    public SampleEventArgs(string text) { Text = text; }
    public string Text { get; } // readonly
}

public class Publisher
{
    // Declare the delegate (if using non-generic pattern).
    public delegate void SampleEventHandler(object sender, SampleEventArgs e);

    // Declare the event.
    public event SampleEventHandler SampleEvent;

    // Wrap the event in a protected virtual method
    // to enable derived classes to raise the event.
    protected virtual void RaiseSampleEvent()
    {
        // Raise the event in a thread-safe manner using the ?. operator.
        SampleEvent?.Invoke(this, new SampleEventArgs("Hello"));
    }
}
//</EventExample>

interface IDrawingObject
{
    event EventHandler OnDraw;
}

//<AddHandler>
class Events : IDrawingObject
{
    event EventHandler PreDrawEvent;

    event EventHandler IDrawingObject.OnDraw
    {
        add => PreDrawEvent += value;
        remove => PreDrawEvent -= value;
    }
}
//</AddHandler>

You don't typically need to provide your own custom event accessors. The automatically generated accessors when you declare an event are sufficient for most scenarios. Starting with C# 14, you can declare partial events. The implementing declaration of a partial event must declare the add and remove handlers.

See also