.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Events in CSharp : Practical View

Posted By:Gaurav Arora       Posted Date: April 28, 2009    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  

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;
namespace CSharp.AStepAhead.events
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. 

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend