Events
class Counter { public event EventHandler ThresholdReached; protected virtual void OnThresholdReached(EventArgs e) { EventHandler handler = ThresholdReached; handler?.Invoke(this, e); } // provide remaining implementation for the class }
Event data
public class ThresholdReachedEventArgs : EventArgs { public int Threshold { get; set; } public DateTime TimeReached { get; set; } }
Event handlers
class Program { static void Main() { var c = new Counter(); c.ThresholdReached += c_ThresholdReached; // provide remaining implementation for the class } static void c_ThresholdReached(object sender, EventArgs e) { Console.WriteLine("The threshold was reached."); } }
References
https://docs.microsoft.com/en-us/dotnet/standard/events/
https://www.tutorialsteacher.com/csharp/csharp-event