Events in CSharp

Events in CSharp

This is a continuation to my previous post: Delegates : A Step Ahead Series

In C# events are based on delegates, with the originator defining one or more call back functions. A call back function is a function in which one piece of code defines another implements, in other words, one piece of code says, “If you implement a function which looks like this, I can call it". A class that wants to sue events defines callback functions or delegates, and the listening object then implements them.

An event is a member that enables an object or class to provide notifications.
As compared to delegates events works with source and listener methodology. So listeners who are interested in receiving some events they subscribe to the source. Once this subscription is done the source raises events to its entire listener when needed. One source can have multiple listeners.

Note: Events have no return type.

/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : msdotnetheaven */
// File name : events.cs
using System;
class Button
public event EventHandler Click;
public void Reset()
Click = null;
public class impButton
Button Button1 = new Button();
public impButton()
Button1.Click += new EventHandler(Button1_Click);

public void Connect()
Console.WriteLine("Attaching the event handler");
Button1.Click += new EventHandler(Button1_Click);
void Button1_Click(object s, EventArgs e)
Console.WriteLine("Button1 clicked!");
public void Disconnect()
Console.WriteLine("Removng the event handler");
Button1.Click -= new EventHandler(Button1_Click);
static void Main()
impButton objButton = new impButton();

Difference between Delegates and Events :

There are following two differences:
1. Events use delegates.
2. Delegates are function pointers they can move across any client.

Article by Gaurav Aroraa
Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

Follow Gaurav Aroraa or read 149 articles authored by Gaurav Aroraa


Author: Sanjay21 Jul 2010 Member Level: Silver   Points : 2

namespace Delegate__event
// This is Two Delegate
public delegate void BPPetrolpump();
public delegate void HPPetrolpump();

// This is class Price

class Price
double _HP_Petrol;
double _HP_Diesal;
double _HP_CNG;
double _BP_Petrol;
double _BP_Diesal;
double _BP_CNG;

// This is two Event
event BPPetrolpump Bharat_petrolium;
event HPPetrolpump Hindustan_Petrolium;

// This is Constractor
public Price()
_HP_Petrol = 53.34;
_HP_Diesal = 48.45;
_HP_CNG = 39.21;
_BP_Petrol = 52.56;
_BP_Diesal = 47.90;
_BP_CNG = 38.52;

// This is first Method
void Price_of_Hindustan_pet()
Console.WriteLine("Price of Hindustan petrolium is:");
Console.WriteLine("HP Price of PETROL is:\t" + _HP_Petrol);
Console.WriteLine("HP Price of DIESAL is:\t" + _HP_Diesal);
Console.WriteLine("HP Price of CNG is:\t" + _HP_CNG);

// This is Second Method
void Price_of_Bharat_pet()
Console.WriteLine("Price of Bharat petrolium is:");
Console.WriteLine("\nBP Price of PETROL is:\t" + _BP_Petrol);
Console.WriteLine("BP Price of DIESAL is:\t" + _BP_Diesal);
Console.WriteLine("BP Price of CNG is:\t" + _BP_CNG);

static void Main(string[] args)
Console.WriteLine(" Price of Petrolium");
Price p1 = new Price();
p1.Bharat_petrolium+=new BPPetrolpump(p1.Price_of_Bharat_pet);
p1.Hindustan_Petrolium += new HPPetrolpump(p1.Price_of_Hindustan_pet);

The return type of the event is alwys dalegate

Author: Gaurav Aroraa22 Jul 2010 Member Level: Gold   Points : 2


Thanks for sharing info. But there is no return type of Events.
Although you can say delegates has as their functions because these are function pointers so here is the hierarchy:
Listener-> Trigger->Function Pointer

Means Events listen and they trigger delegate which points to functions/methods.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: