Skip to content

Events

Context: Events enable a class to notify other classes when something happens. They are built on delegates and provide a publish‑subscribe pattern.

Events are declared using the event keyword and a delegate type. Only the declaring class can invoke the event; subscribers can only add/remove handlers.

public class Button
{
public event EventHandler Clicked;
protected virtual void OnClicked() => Clicked?.Invoke(this, EventArgs.Empty);
}

UI frameworks: Buttons, text boxes, and other controls expose events like Click, TextChanged, MouseMove that your code can subscribe to.

Example: In WinForms, every control has events. ASP.NET Core Blazor also uses events for component communication.