// Declare event public event EventHandler<string> MyEvent; // +-----------^------------+ // | Type of event argument | // +----------------------v-+ // Create a handler void MyClass_MyEvent(object sender, string e) { Console.WriteLine(e); } // Subscribe handler to event (multiple subscriptions possible) MyEvent += MyClass_MyEvent; // Trigger/invoke/execute event MyEvent?.Invoke(null, "EventData");
Events are examples of the Observer Pattern
345100cookie-checkC# Basic Events