C# - Handling an Event Declared in an Interface Last Updated : 26 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Events and event handlers are generally used in case of a Publisher-Subscriber design pattern where some kind of event has occurred in a particular class and it needs to notify other classes about the event. The Publisher class has an event which gets invoked on some condition, the subscribers are notified based on their subscription. One Key benefit of this is the publisher does not need to know anything about who gets notified. Syntax :event EvenHandler handler_name; EventHandler is a delegate. To subscribe to this event a class must have a method with void return type and two input parameters of types : Object - The source or publisher who invoked the event. EventArgs - An object class which contains the event information. Syntax for Delegate Method:public void method_name(Object sender, EventArgs e); Note: The Subscriber must have a method of same signature as described above in order to receive the event notification from the publisher. Syntax for Subscribe to an Event:publisher.eventhandler_name += new EventHandler(subscriber_method_with_same_signature); Syntax to Unsubscribe from an Event:publisher.eventhandler_name -= new EventHandler(subscriber_method_with_same_signature); Let's understand this with an example. Suppose we have a Channel class and two Subscriber classes. Whenever a video is uploaded on the channel. Both the subscribers should get the notification about the video upload. In this case Channel will work as a Publisher and raise the event. The subscribers who have subscribed to the event will receive the event raised through event handlers. Key Logic : The Channel class will have an event. We will trigger it at some condition.Subscriber will have a method to receive the event. We will attach(subscribe) this method to the channel class's event, so that whenever the event is invoked the subscriber's method gets executed automatically.Example 1: C# // C# program for Illustrating Handling an // Event Declared in an Interface through C# using System; using System.Collections.Generic; namespace GFG { // <summary> // Video class // </summary> public class Video { public string Name; } // Interface IChannel has an event VideoAdded public interface IChannel { event EventHandler VideoAdded; } // Channel class - A Publisher of the event public class Channel : IChannel { public event EventHandler VideoAdded; public string Name { get; set; } public List<Video> ListOfVideos = new List<Video>(); public void AddVideo() { Video video = new Video(); video.Name = "EventHandler in c#"; ListOfVideos.Add(video); // Notify all the subscribers NotifyAllSubscribers(); } public void NotifyAllSubscribers() { EventHandler handler = VideoAdded; // safety check to avoid null reference error if (handler != null) { // Invoking the event VideoAdded. // 1st parameter - sender object i.e Channel // class in this case 2nd parameter - object of // type EventArgs() handler(this, new EventArgs()); } } } // receiver 1 public class Subscriber_A { public string Name { get; set; } public Subscriber_A(Channel ch) { // Subscribing to the publisher's event ch.VideoAdded += new EventHandler(ReceiveVideoNotification); } // Method that is to be executed when the event is // triggered. public void ReceiveVideoNotification(object sender, EventArgs e) { Console.WriteLine( "Hello {0}, a new video has been uploaded by GeeksForGeeks", Name); Console.WriteLine(); } } // receiver 2 public class Subscriber_B { public string Name { get; set; } public Subscriber_B(Channel channel) { // Subscribing to the publisher's event channel.VideoAdded += new EventHandler(ReceiveVideoNotification); } // Method that is to be executed when the event is // triggered. public void ReceiveVideoNotification(object sender, EventArgs e) { Console.WriteLine( "Hello {0}, a new video has been uploaded by GeeksForGeeks", Name); Console.WriteLine(); } } class Program { static void Main(string[] args) { Channel channel = new Channel(); channel.Name = "GeeksForGeeks"; Subscriber_A A = new Subscriber_A(channel); A.Name = "Geek_1"; Subscriber_B B = new Subscriber_B(channel); B.Name = "Geek_2"; // Adding a video channel.AddVideo(); } } } Output: Note : The order in which the subscriber objects are created will be preserved while receiving event notification. Comment More infoAdvertise with us Next Article C# - Handling an Event Declared in an Interface B biswalashish1997 Follow Improve Article Tags : C# CSharp-Interfaces CSharp-programs Similar Reads Event Handling in Android Events are the actions performed by the user in order to interact with the application, for e.g. pressing a button or touching the screen. The events are managed by the android framework in the FIFO manner i.e. First In - First Out. Handling such actions or events by performing the desired task is c 5 min read Event Handling in Java An event is a change in the state of an object triggered by some action such as Clicking a button, Moving the cursor, Pressing a key on the keyboard, Scrolling a page, etc. In Java, the java.awt.event package provides various event classes to handle these actions.Classification of Events Events in J 5 min read Difference between Abstract Class and Interface in C# An abstract class is a way to achieve abstraction in C#. To declare an abstract class, we use the abstract keyword. An Abstract class is never intended to be instantiated directly. This class must contain at least one abstract method, which is marked by the keyword or modifier abstract in the class 4 min read C# | Explicit Interface Implementation An Interface is a collection of loosely bound items that have a common functionality or attributes. Interfaces contain method signatures, properties, events etc. Interfaces are used so that one class or struct can implement multiple behaviors. C# doesn't support the concept of Multiple Inheritance b 4 min read Delegates vs Interfaces in C# A Delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which method is to be called when an event is triggered. Example: CSha 3 min read How to Access Interface Fields in Golang? Go language interfaces are different from other languages. In Go language, the interface is a custom type that is used to specify a set of one or more method signatures and the interface is abstract, so you are not allowed to create an instance of the interface. But you are allowed to create a varia 3 min read Input Events in Android with Example In Android, there's quite a method to intercept the events from a user's interaction with your application. When considering events within your interface, the approach is to capture the events from the precise View object that the user interacts with. The View class provides the means to try to do s 7 min read Disabling and Enabling an Interface on Linux System In the intricate web of Linux systems, network interfaces serve as the lifelines that connect our machines to the vast digital world. Understanding how to effectively disable and enable these interfaces is a crucial skill for Linux administrators and users alike. In this detailed guide, we will expl 5 min read Default Interface Methods in C# 8.0 Before C# 8.0 interfaces only contain the declaration of the members(methods, properties, events, and indexers), but from C# 8.0 it is allowed to add members as well as their implementation to the interface. Now you are allowed to add a method with their implementation to the interface without break 5 min read Implement Interface using Abstract Class in Java Interface contains only abstract methods that can't be instantiated and it is declared by keyword interface. A class that is declared with the abstract keyword is known as an abstract class in Java. This is a class that usually contains at least one abstract method which can't be instantiated and It 3 min read Like