You mentioned the syntax for adding / removing events in some user control examples. Most likely, these examples using the UserControl class' Events property to store event handlers, for example, in the following example:
public event EventHandler MyEvent { add { Events.AddHandler("MyEvent", value); } remove { Events.RemoveHandler("MyEvent", value); } }
The idea is that usually the user of the control does not want to handle every event that the control provides. If each event is defined as a "field" event (as in your example), then each event will occupy a piece of memory, even if there are no subscribers for this event. When you have a complex page consisting of hundreds of controls, each of which can have dozens of events, the memory consumption for unused events is not negligible.
This is why the System.ComponentModel.Component class (the base class of the System.Windows.Forms.Control class) has the Events property, which is basically a dictionary for storing event handler delegates. Thus, each event is implemented more as a property than a field. Add / remove handlers for each event store or remove delegates from the Events dictionary. If an event is not used, then there is no word for it in the Events dictionary, and additional memory is not consumed for this event. This is a compromise in that you do a little more work (you need to look for an event handler) to save a little more memory.
EDIT: my answer is for Windows Forms, not ASP.NET, although the concepts are the same.
Dr. Wily's apprentice
source share