advertisement
Events in C# Programming
Events
Consider a group of people at a party playing Bingo. When a number is called, the participants check if the number is on their cards whereas the non-participants go about their business, enjoying other activities. If this situation is analyzed from a programmer's perspective, the calling of the number corresponds to the occurrence of an event. The notification about the event is given by the announcer. Here, the people playing the game are paying attention (subscribing) to what the announcer (the source of the event) has to say (notify).
Similarly, in C#, events allow an object (source of the event) to notify other objects (subscribers) about the event (a change has occurred).
Features
An event is a user-generated or system-generated action that enables the required objects to notify other objects or classes to handle the event. Events in C# have the following features:
- They can be declared in classes and interfaces.
- They can be declared as abstract or sealed.
- They can be declared as virtual.
- They are implemented using delegates.
Events can be used to perform customized actions that are not already supported by C#. Events are widely used in creating GUI-based applications, where events such as selecting an item from a list and closing a window are tracked.
Creating and Using Events
There are four steps for implementing events in C#. These are as follows:
- Define a public delegate for the event.
- Create the event using the delegate.
- Subscribe to listen and handle the event.
- Raise the event.
Events use delegates to call methods in objects that have subscribed to the event. When an event containing several subscribers is raised, many delegates will be invoked.
Declaring Events
An event declaration consists of two steps, creating a delegate and creating the event. A delegate is declared using the delegate keyword. The delegate passes the parameters of the appropriate method to be invoked when an event is generated. This method is known as the event handler. The event is then declared using the event keyword followed by the name of the delegate and the name of the event. This declaration associates the event with the delegate.
Raising Events
An event is raised to notify all the objects that have subscribed to the event. Events are either raised by the user or the system. Once an event is generated, all the associated event handlers are executed. The delegate calls all the handlers that have been added to the event. However, before raising an event, you need to create handlers and thus, make sure that the event is associated with the appropriate event handlers. If the event is not associated with any event handler, the declared event is considered to be null.
Events and Inheritance
Events in C# can only be invoked in the class in which they are declared and defined. Therefore, events cannot be directly invoked by the derived classes. However, events can be invoked indirectly in C# by creating a protected method in the base class that will, in turn, invoke the event defined in the base class.
Types of Events
Here are the 7 main types of events in C#:
1. Instance Events:
- Definition: These are occasions declared inside a category and raised through its times.
- Example: A Button control raising a Click event while clicked.
2. Static Events:
- Definition: These are events declared within a class but raised by the magnificence itself, no longer its instances.
- Example: A logging class elevates a LogMessage event every time a message is logged.
3. Multicast Events:
- Definition: These events can have more than one subscriber (listeners) registered to them.
- Example: A TextBox manages elevating a TextChanged occasion, notifying all subscribed listeners about the text alternate.
4. Delegate-Based Events:
- Definition: These events are primarily based on delegates, which are characteristic suggestions that point to the subscriber's event handler techniques.
- Example: A Timer manages to raise a Tick occasion using a delegate to invoke subscribed occasion handler techniques for each tick.
5. Virtual and Abstract Events:
- Definition: These occasions can be declared with the virtual keyword, allowing derived instructions to override them and provide their very own event conduct.
- Example: A base class affirming a virtual OnStart event that derived classes can override and enforce differently.
6. Sealed Events:
- Definition: These occasions are declared with the sealed keyword, preventing derived training from overriding them.
- Example: An occasion used internally using a framework that desires to have a specific conduct and can not be overridden.
7. Anonymous Events:
- Definition: These occasions can be declared the usage of nameless delegates, taking into account dynamic event advent without defining a separate delegate kind.
- Example: An event used for inner communique between objects within a specific context.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0