10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

Events in C#

Updated on Sep 3, 2025
 
45,996 Views
Table of Content

Events in C# are actions that allow classes or objects to inform other classes or objects when an interesting phenomenon occurs.

Some of the properties of events are given as follows:

  1. Events have publishers and subscribers. The publishers determine when an event is raised while the subscribers determine the actions that are performed in response to the event.
  2. If there are no subscribers for an event, then event is never raised. However, for an event there can be many subscribers.
  3. If an event has many subscribers, when the event is raised the event handlers are invoked synchronously.
  4. User actions such as menu selections, button clicks etc. are signaled by using events.
  5. The events are based on the EventHandler delegate and the EventArgs base class.

Event Syntax

The events are declared using the keyword event. The syntax for this is given as followed:

event delegate_name event_name;

In the above syntax, the keyword event is followed by the delegate name and then the event name.

A program that demonstrates events is given as follows:

using System;
namespace Example {
  public delegate string demoDelegate(string str1, string str2);
  class MyEvents {
     event demoDelegate myEvent;
     public MyEvents() {
        this.myEvent += new demoDelegate(this.Display);
     }
     public string Display(string studentname, string subject) {
        return "Student: " + studentname + "\nSubject: " +subject;
     }
     static void Main(string[] args) {
        MyEvents e = new MyEvents();
        string res = e.myEvent("Jack", "Science");
        Console.WriteLine("RESULT...\n"+res);
     }
  }
}

Source Code: Program to implement Events in C#

The output of the above program is as follows:

RESULT...
Student: Jack
Subject: Science
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses