Do you know that? 25% freelancers are 1-2 years as a freeleancer next

Describe multicast event handling with example.

Ans

using System;
namespace Test
{
// Declare a delegate type for an event.
delegate void MyEventHandler();
// Declare a class that contains an event.
class MyEvent
{
public event MyEventHandler SomeEvent;
// This is called to fire the event.
public void OnSomeEvent()
{
if (SomeEvent != null)
SomeEvent();
}
}
class ButtonClass
{
public void Buttonhandler()
{
Console.WriteLine("Event received by button object");
}
}
class CheckBoxClass
{
public void Chkhandler()
{
Console.WriteLine("Event received by checkbox object");
}
}
class EventDemo
{
static void Handler()
{
Console.WriteLine("Event received by EventDemo");
}
static void Main()
{
MyEvent evt = new MyEvent();
ButtonClass btnObj = new ButtonClass();
CheckBoxClass chkObj = new CheckBoxClass();
// Add handlers to the event list.
evt.SomeEvent += Handler;
evt.SomeEvent += btnObj.Buttonhandler;
evt.SomeEvent += chkObj.Chkhandler;
// Fire the event.
evt.OnSomeEvent();
Console.WriteLine();
// Remove a handler.
evt.SomeEvent -= btnObj.Buttonhandler;
evt.OnSomeEvent();
}
}
}

Output:

Event received by EventDemo
  Event received by button object
  Event received by checkbox object
  Event received by EventDemo
  Event received by checkbox object

Important points regarding event.

Events can be specified in interfaces. Implementing classes must supply the event.
Events can be specified as abstract
An event can be specified as sealed.
An event can be virtual.