Add and Remove Accessors

With events based on a piece of metadata pointing at two methods, it shouldn’t be at all surprising that C# provides a way to customize those methods in case you want to do so.

In the context of events (and properties), we refer to those methods as accessors. For events, two accessors exist called add and remove. The syntax is predictable, being analog to get and set for properties:

public event Finished {    add {        // Called when doing +=.    }    remove {        // Called when doing -=.    }}

Inside those accessors, the right side of the += and -= operator is available using the value keyword. This is similar to the way a property setter gets access to the value being assigned to it.

Once we implement add and ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.