C# Events Tutorial For Beginners With Examples

An event is a way to notify same or other classes on the occurrence of an action.
The action can be click of a button in a .NET application, key pressed in a keyboard, a mouse click and so on.

Declaring Events

In C# Events are declared using Delegates. So to declare an Event first a Delegate type has to be declared.
In the below code a PrintHandler Delegate type has been created.
    public delegate void PrintHandler();
        
Once a Delegate type is available an Event can be declared as below:
    public static event PrintHandler PrintEvent;       

Triggering Events

An Event can be triggered by calling it from a method. So we can create a method called asInvokePrint which will trigger the PrintEvent.
    public static void InvokePrint()
    {
        if (PrintEvent != null)
        {
            //Trigger the event 
            PrintEvent();
        }
    }            
        
When an Event is raised a method called as Eventhandler is called to handle that Event. An Eventhandler performs a specific activity on occurence of any event.
In our case when a PrintEvent is triggered an Eventhandler PrintMessage should be invoked. Our PrintMessage method will write a message to the console window.
    private static void PrintMessage()
    {
        Console.WriteLine("Hello from Events");
    }
        
An Eventhandler has to be attached to an Event for it to be invoked when the Event is triggered.
The Eventhandler PrintMessage is attached to the Event PrintEvent as shown in the below code:
    PrintEvent += new PrintHandler(PrintMessage);    
        
Lets put everything together and see how it looks like.
    
    class MainEvent
    {
        //Declare a delegate
        public delegate void PrintHandler();

        //Declare an event of type PrintHandler delegate
        public static event PrintHandler PrintEvent;

        public static void InvokePrint()
        {
            if (PrintEvent != null)
            {
                //Trigger the event 
                PrintEvent();
            }
        }

        private static void PrintMessage()
        {
            Console.WriteLine("Hello from Events");
        }

        static void Main(string[] args)
        {
            PrintEvent += new PrintHandler(PrintMessage);

            //Trigger the event by calling this method
            InvokePrint();

            Console.ReadLine();
        }
    }
        
Output:
Hello from Events
Note that in the examples used in this chapter the static keyword is used for Events and Methods so that it can be called from static Main method.
The above example uses a single class to create the Event and Eventhandler. However, its possible to declare Events in one class and Eventhandlers in another class. These type of classes are known as Publisher and Subscriber classes.
Publisher Class:
The class in which the Event, the Delegate and the method to trigger the Event is declared.
Subscriber Class:
The class in which the Eventhandler is declared and a call is made to the publisher class to raise the event.