What are events in C#?

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system-generated notifications.

The events are declared and raised in a class and associated with the event handlers using delegates within the same class or some other class. The class containing the event is used to publish the event.

To declare an event inside a class, first a delegate type for the event must be declared. For example,

public delegate string myDelegate(string str);

Now, declare an event −

event myDelegate newEvent;

Now let us see an example to work with events in C# −


 Live Demo

using System;

namespace Demo {
   public delegate string myDelegate(string str);

   class EventProgram {
      event myDelegate newEvent;

      public EventProgram() {
         this.newEvent += new myDelegate(this.WelcomeUser);

      public string WelcomeUser(string username) {
         return "Welcome " + username;

      static void Main(string[] args) {
         EventProgram obj1 = new EventProgram();
         string result = obj1.newEvent("My Website!");


Welcome My Website!

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started