Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Operators and Expressions

The += and -= operators on events

Reference for C# += and -= on events: subscribing and unsubscribing handlers, lambdas, invocation order, memory leaks and why only the owner can raise it.

Outside the class that declares an event, the only things you can do with it are += (subscribe a handler) and -= (unsubscribe it). You cannot assign with =, read the list of handlers, or invoke the event. That restriction is what makes events safer than a public delegate field.

A handler is any method or lambda whose signature matches the event's delegate type. When the owner raises the event, all subscribed handlers run, in the order they were added.

C#
button.Clicked += OnClicked;
button.Clicked -= OnClicked;

Try it

C# Subscribing and unsubscribing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Lifetime, errors and the owner's view

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
eventExpression += handler;
eventExpression -= handler;
Name Description
eventExpression An event member, accessed through an object or type.
handler A method group, a lambda, an anonymous method or a delegate instance of the event's type.

Returns: nothing; it is a statement, not a value-producing expression.

Errors: a handler with the wrong signature is CS0123 for method groups. Using =, calling, or reading the event from outside its class is CS0070.

Usage notes

  • += on a field-like event calls the compiler-generated add accessor, which combines delegates in a thread-safe way. -= calls remove. Custom events let you write these two accessors yourself.
  • -= removes the last matching instance in the invocation list. Removing a handler that is not there does nothing and does not throw.
  • To unsubscribe a lambda you must keep it in a variable. A new lambda with the same text is a different delegate.
  • Forgotten subscriptions are a classic memory leak: the publisher holds a reference to the subscriber, so a short-lived listener on a long-lived publisher is never collected. Unsubscribe in Dispose or use weak events.
  • Handlers run synchronously on the thread that raises the event, one after another. An exception in one stops the rest unless the publisher invokes each handler separately.
  • Subscribing to a null event field works; the compiler handles that case for you.
  • An async void handler is allowed for events and is the one common place it is acceptable, but exceptions in it cannot be caught by the publisher.
  • Inside the declaring class the event name refers to the underlying delegate, so you can invoke it with Tick?.Invoke(n).
  • Static events live as long as the application. Be extra careful with subscriptions to them.