Publisher and Subscriber Events
Events expose subscription while reserving publication for the owning class.
How it works
Notify subscribers when an order changes.
The null-conditional invocation safely does nothing when an event has no subscribers.
Runnable C# example
Program.cs
using System;
class Order
{
public event EventHandler<string>? StatusChanged;
public void Update(string status) => StatusChanged?.Invoke(this, status);
}
class Program
{
static void Main()
{
var order = new Order();
order.StatusChanged += (_, status) => Console.WriteLine(status);
order.Update("Shipped");
}
}
ShippedPractice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.