The Event is something special that is going to happen. Here we will take an example of an event, where Microsoft launches the events for the developer. In this Event, Microsoft wants to aware the developer about the feature of the existing or new products. For this, Microsoft will use Email or other advertisement options to aware the developer about the Event. So, in this case, Microsoft will work as a publisher who raises the Event and notifies the developers about it. Developers will work as the subscriber of the Event who handles the Event.
Similarly, in C#, Events follow the same concept. In C#, Event can be subscriber, publisher, subscriber, notification, and a handler. Generally, the User Interface uses the events. Here we will take an example of Button control in Windows. Button performs multiple events such as click, mouseover, etc. The custom class contains the Event through which we will notify the other subscriber class about the other things which is going to happen. So, in this case, we will define the Event and inform the other classes about the Event, which contains the event handler.
The event is an encapsulated delegate. C# and .NET both support the events with the delegates. When the state of the application changes, events and delegates give the notification to the client application. Delegates and Events both are tightly coupled for dispatching the events, and event handling require the implementation of the delegates. The sending event class is known as the publisher, and the receiver class or handling the Event is known as a subscriber.
The key points about the events are as:
public event EventHandler CellEvent;
For the declaration of the Event in the class, firstly, the event type of the delegate must be declared.
public delegate void CellEventHandler(object sender, EventArgs e);
public event CellEventHandler CellEvent;
if (CellEvent != null) CellEvent(this, e);
We can invoke the Event only from within the class where we declared the Event.
OurEventClass.OurEvent += new ChangedEventHandler(OurEventChanged);
OurEventClass.OurEvent -= new ChangedEventHandler(OurEventChanged);
Delegates work as pointer to a function. It is a reference data type and it holds the reference of the method. System.Delegate class implicitly derived all the delegates.
Delegate can be declared using the delegate keyword which is followed by the signature
<access modifier> delegate <return type> <delegate_name>(<parameters>)
public delegate void PrintWord(int value);
The above PrintWord delegate can be used to point any method which has the same return type and declared parameters with PrintWord. Here we will take an example that declares and uses the PrintWord delegates.
class Program1 { // declare delegate public delegate void PrintWord(int value); static void Main(string[] args) { // Print delegate points to PrintNum PrintWord printDel = PrintNum; // or // Print printDel = new Print(PrintNumber); printDel(100000); printDel(200); // Print delegate points to PrintMoney printDel = PrintMoney; printDel(10000); printDel(200); } public static void PrintNum(int num) { Console.WriteLine("Number: {0,-12:N0}",num); } public static void PrintMoney(int money) { Console.WriteLine("Money: {0:C}", money); } }
In the above example, we declared the PrintWord delegates, which accepts the int type parameter and returns the void. In the main() method, we declare the PrintWord type method and assigned the PrintNum name method. Now we will invoke the PrintWord delegate, which in-turn invokes the PrintNum method. In the same way, if the PrintWord delegates variable is assigned to the PrintMoney method, then this will invoke the PrintMoney method.
Also, we can create the delegate object by using the new operator and specify the name of the method, as shown below:
PrintWord printDel = new PrintWord(PrintNum);
Delegates can be declared, as shown below:
public delegate void someEvent(); public organize event