C# Basic Events

Date: 2020-02-19
// 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

34510cookie-checkC# Basic Events