In c#, the event is a message sent by an object to indicate that particular action will happen. The action could be caused either by a button click, mouse movements, or other programming logic. The object that raises an event is called an event sender.
In simple words, we can say that events are used to signal user actions such as button click, mouse over, menu selection, etc., in the user interface of windows and web applications.
Generally, an event is nothing but something special that is going to happen. For example, Microsoft or Google will conduct Build / Keynote events to announce their new features or products. They will notify about their events to the users either through email or advertisements. Here Google or Microsoft is a publisher who conducts (or raises) an event and users become a subscriber to attend (or handle) the event.
In c#, events will follow the same pattern, and it will contain a publisher, subscriber, notification, and handler. The events will enable a class or object to notify other classes or objects when something special happens. The class that sends (or raises) an event is called the publisher, and the class that receives (or handle) an event is called subscriber.
In c#, the publisher will determine when an event is raised, and the subscribers will determine what action can be taken in response to the event. An event in c# will have multiple subscribers, and the events that have no subscribers will never be raised. The subscriber can handle multiple events from multiple publishers.
Now we will see how to create and use events in c# to notify other classes or objects when something of interest happens.
Following is the example of declaring an event using
event keyword in c# programming language.
In c#, to raise an event, we need to invoke the event delegate and subscribe to an event using
+= operator. If you want to unsubscribe from an event, then use
To respond to an event, we need to define an event handler method in the event receiver, and this method signature must match with the event delegate's signature. In the event handler, you can perform actions that are required whenever the event is raised, such as getting the user input after a click on the button.
Following is the example of declaring and raising an event using the underlying delegate type in the c# programming language.
If you observe the above example, we created a Maths class (publisher) and Operations class (subscriber) with the required methods and SampleEvent event. In Maths class methods, we check the condition like whether the calling class subscribed to the SampleEvent event or not to execute the required functionality.
In the Operations class, we subscribed to the SampleEvent event using
+= operator and mentioned a handler's name (SampleEventHandler) to perform the required operations when an event is raised.
If you observe the handler method (SampleEventHandler) in the Operations class, it has the same signature as our delegate (SampleDelegate) in the Maths class.
When you execute the above c# program, you will get the result below.
The following are the important points that we need to remember about events in the c# programming language.
eventkeyword with delegate type.
+=operator, we can subscribe to an event, and by using
-=operator, we can unsubscribe from an event.