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
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
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter