- Events help two objects communicate with each other or be notified when required.
- Events help in building loosely-coupled applications which are extensible.
Dopper dopper = new Dopper();
dopper.DopperDopped += delegate (object o, EventArgs e)
{
Console.WriteLine("Dopper Event Handled");
};
dopper.DoDope();
class Dopper
{
public event EventHandler<EventArgs> DopperDopped;
public void DoDope()
{
CallRaiser();
}
protected virtual void CallRaiser()
{
if (DopperDopped != null)
{
Console.WriteLine("Raising Event");
DopperDopped(this, EventArgs.Empty);
}
}
}
Related