An event is a message or you can say a notification that is sent by an object to signal the occurrence of an action.
Let's consider the following example, we declare an event named ThresholdReached
. The event is associated with the EventHandler
delegate and raised in a method named OnThresholdReached
.
public event EventHandler ThresholdReached;
protected virtual void OnThresholdReached(EventArgs e)
{
EventHandler handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
}
C# provides the EventHandler
and EventHandler<TEventArgs>
delegates to support most event scenarios. The following code shows how to raise the event.
class Counter
{
public event EventHandler ThresholdReached;
protected virtual void OnThresholdReached(EventArgs e)
{
EventHandler handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
}
private int total;
public void Add(int x)
{
total += x;
Console.WriteLine("Count: "+ total);
if (total == 7)
{
Console.WriteLine("Event raised...");
OnThresholdReached(EventArgs.Empty);
}
}
}
It contains a class named Counter
that has an event named ThresholdReached
. This event is raised when a counter value equals to 7
. When the event is raised, it will be handled by the following method.
static void c_ThresholdReached(object sender, EventArgs e)
{
Console.WriteLine("Event received.");
}
The above example shows an event handler method named c_ThresholdReached
that matches the signature for the EventHandler
delegate.
The following code registers the ThresholdReached
event and starts the counter and increment it in a for
loop.
Counter c = new Counter();
c.ThresholdReached += c_ThresholdReached;
for (int i = 0; i < 10; i++)
{
c.Add(1);
}
Let's run the above code and you will see the following code.
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Count: 6
Count: 7
Event raised...
Event received.
Count: 8
Count: 9
Count: 10
For more information about events, visit https://docs.microsoft.com/en-us/dotnet/standard/events/
All the examples related to the events are available in the Events.cs
file of the source code. Download the source code and try out all the examples for better understanding.