SIGN UP MEMBER LOGIN:    
ARTICLE

Delegates and Events in C#

Posted by Praveen Kumar Articles | Visual C# November 25, 2009
In this article I will explain about Delegates and Events in C#.
Reader Level:
Download Files:
 

This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.

A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature. A delegate is thus equivalent to a type-safe function pointer or a callback. Although delegates have other uses, the discussion here focuses on the event-handling functionality of delegates.

Events in C# are handled by delegates, which serve as a mechanism that defines one or more callback functions to process events. An event is a message sent by an object to signal the occurrence of an action. The action could arise from user interaction, such as a mouse click, or could be triggered by some other program logic. The object that triggers the event is called the event sender. The object that captures the event and responds to it is called the event receiver.

In event communication, the event sender class does not know which object or method will handle the events it raises. It merely functions as an intermediary or pointer-like mechanism between the source and the receiver, as illustrated in Listing 5.47. The .NET framework defines a special type delegate that serves as a function pointer.

Listing 5.47: DelegateEvent.cs, Delegates and Events Example


using
System;
public
class MyEvt
{
    public delegate void t(Object sender, MyArgs e);

    // declare a delegate

    public event t tEvt; //declares an event for the delegate

    public void mm()
    {
    //function that will raise the callback
        MyArgs r = new MyArgs();
        tEvt(this, r); //calling the client code
    }

    public MyEvt()
    {
    }

}


//arguments for the callback


public
class MyArgs : EventArgs
{
    public MyArgs()
    {
    }
}


public
class MyEvtClient
{
    MyEvt oo;

    public MyEvtClient()
    {
        this.oo = new MyEvt();
        this.oo.tEvt += new MyEvt.t(oo_tt);
    }

    public static void Main(String[] args)
    {
        MyEvtClient cc = new MyEvtClient();
        cc.oo.mm();
    }

    //this code will be called from the server

    public void oo_tt(object sender, MyArgs e)
   {
        Console.WriteLine("yes");
        Console.ReadLine();
    }
}


fig-5.10.gif

Figure 5.10: Screen Output Generated from Listing 5.47

Conclusion

Hope this article would have helped you in understanding Delegates and Events in C#. See other articles on the website on .NET and C#.

visual C-sharp.jpg The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor