SIGN UP MEMBER LOGIN:    
ARTICLE

Delegates in C#

Posted by Rekha Articles | C# Language July 24, 2009
In this article let us learn about Delegates in C#.
Reader Level:
Download Files:
 

what are Delegates ?

  • In simple words we can say delegates are a .NET object which points to a method that matches its specific signature.
  • Delegates add a safety dimension in handling function pointers in .NET.
  • Delegates are type-safe, object oriented, secure .NET objects which can be used to invoke methods of matching signature.
  • While using delegates it is very much necessary to make sure that the functions which the delegates points has the same number of argument type and same return type. For example if we have a method that takes a single string as a parameter and another method that takes two string parameters, then we need to have two separate delegate type for each method.
  • A delegate can be used to invoke a method, the call to which can only be resolved or determined at runtime.
  • Delegates takes method as parameter
Types of Delegates
  • Single Cast Delegate
  • Multi Cast Delegate
Single Cast Delegate: This is a kind of delegate that can refer to single method at one time.

SingleCast Delegates refer to a single method with matching signature. SingleCast Delegates derive from the System.Delegate class

Multi Cast Delegate: This is a kind of delegates that can refer to multiple methods that have the same signature at one time.

Syntax of Declaring a Delegate:

public delegate returntype delegatename(datatype varname);

Example:

public delegate void dele(int x, int y);

Program using Single Cast Delegate:

using System;
using
System.Collections.Generic;
using
System.Text; 

namespace DelegateEX
{
    public delegate void mydele(int x, int y);

    class A
    {
        public void add(int x, int y)
        {
            Console.WriteLine("The Sum is " + (x + y));
        } 

        public void sub(int x, int y)
        {
            Console.WriteLine("The Difference is " + (x - y));
            Console.ReadKey();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A obj = new A();
            //make an object of class A
            mydele m = new mydele(obj.add);
            m(10, 20);
            mydele m1 = new mydele(obj.sub);
            m1(10, 20);
        }
    }
}

singleDelegateoutput.bmp

In the above program we created a delegate named mydele having two parameters of type int and is not returning any value. In Main function we created its two objects m and m1 pointing add and sub function of class A respectively.

Now let's discuss the concept of using Multicast delegate in which one delegate points to multiple methods at one time.

Program using Multi Cast Delegate:

using System;
using
System.Collections.Generic;
using
System.Text;
namespace
DelegateEX
{
    public delegate void mydele(int x, int y);

    class A
    {
        public void add(int x, int y)
        {
            Console.WriteLine("The Sum is " + (x + y));
        } 

        public void sub(int x, int y)
        {
            Console.WriteLine("The Difference is " + (x - y));
            Console.ReadKey();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A b1 = new A();
            //make an object of class A
            myMultiDele Md = new myMultiDele(b1.add);
            myMultiDele Md1 = new myMultiDele(b1.sub);
            Md = Md + Md1;
            Md(60, 90);
        }
    }
}

Output

MultiDelegateoutput.bmp
 

Login to add your contents and source code to this article
share this article :
post comment
 

This is very nice
 so nice of u
so thanks for this.
If u dont mine can u send me details about master page.

Posted by kishore babu Aug 20, 2010

very useful for the beginners.

Posted by mahi reddy Mar 26, 2010

Hi,
Thanks a lot to post this article...
It was helpful for me to understand abt Delegate....the article is Simply Supper to understand delegate

Posted by THANGA KUMAR Sep 23, 2009

I was looking for a C# version of this thanks alot

for those looking for a VB.NET version of Delegates .. read here


Posted by Hussein Nasser Jul 27, 2009

You explain this in PLAIN AND SIMPLE ENGLISH, thank you for that.

Posted by Dan Jul 26, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
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.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor