Click here to Skip to main content
14,332,463 members

Understanding Delegates in C#

Rate this:
4.33 (173 votes)
Please Sign up or sign in to vote.
4.33 (173 votes)
15 Sep 2005CPOL
This article describes delegates in C#.


Delegate is like a buzz word in C#.NET programming. In this article, I explain delegates, multicast delegates and their usage with the help of simple C# programs.

What is a Delegate?

Delegate is a type which  holds the method(s) reference in an object. It is also referred to as a type safe function pointer.


  • Encapsulating the method's call from caller
  • Effective use of delegate improves the performance of application
  • Used to call a method asynchronously


public delegate type_of_delegate delegate_name()


public delegate int mydelegate(int delvar1,int delvar2)


  • You can use delegates without parameters or with parameter list
  • You should follow the same syntax as in the method
    (If you are referring to the method with two int parameters and int return type, the delegate which you are declaring should be in the same format. This is why it is referred to as type safe function pointer.)

Sample Program using Delegate

public delegate double Delegate_Prod(int a,int b);
class Class1
    static double fn_Prodvalues(int val1,int val2)
        return val1*val2;
    static void Main(string[] args)
        //Creating the Delegate Instance
        Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
        Console.Write("Please Enter Values");
        int v1 = Int32.Parse(Console.ReadLine());
        int v2 = Int32.Parse(Console.ReadLine());
        //use a delegate for processing
        double res = delObj(v1,v2);
        Console.WriteLine ("Result :"+res);


Here I have used a small program which demonstrates the use of delegate.

The delegate "Delegate_Prod" is declared with double return type and accepts only two integer parameters.

Inside the class, the method named fn_Prodvalues is defined with double return type and two integer parameters. (The delegate and method have the same signature and parameter type.)

Inside the Main method, the delegate instance is created and the function name is passed to the delegate instance as follows:

Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);

After this, we are accepting the two values from the user and passing those values to the delegate as we do using method:


Here delegate object encapsulates the method functionalities and returns the result as we specified in the method.

Multicast Delegate

What is Multicast Delegate?

It is a delegate which holds the reference of more than one method.

Multicast delegates must contain only methods that return void, else there is a run-time exception.

Simple Program using Multicast Delegate

delegate void Delegate_Multicast(int x, int y);
Class Class2
    static void Method1(int x, int y)
        Console.WriteLine("You r in Method 1");

    static void Method2(int x, int y)
        Console.WriteLine("You r in Method 2");

    public static void <place w:st="on" />Main</place />()
        Delegate_Multicast func = new Delegate_Multicast(Method1);
        func += new Delegate_Multicast(Method2);
        func(1,2);             // Method1 and Method2 are called
        func -= new Delegate_Multicast(Method1);
        func(2,3);             // Only Method2 is called


In the above example, you can see that two methods are defined named method1 and method2 which take two integer parameters and return type as void.

In the main method, the Delegate object is created using the following statement:

Delegate_Multicast func = new Delegate_Multicast(Method1);

Then the Delegate is added using the += operator and removed using the -= operator.


This article would help to understand delegates in C# using simple examples. If you have any query regarding this article, you can reach me at


  • 16th September, 2005: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Arul Nayagam C
Web Developer
India India
Working as a Software Engineer in a MNC Company.Knowledge on C#.NET, ASP.NET, VB.NET ,VB & SQL Server.

Comments and Discussions

GeneralMy vote of 1 Pin
sarveshk.net21-Jul-11 0:27
membersarveshk.net21-Jul-11 0:27 
GeneralSimplest article on delegates! Pin
Member 765836712-Mar-11 1:34
memberMember 765836712-Mar-11 1:34 
GeneralMy vote of 3 Pin
Ashish96-Feb-11 18:17
memberAshish96-Feb-11 18:17 
GeneralMy vote of 4 Pin
Christy A C31-Jan-11 0:01
memberChristy A C31-Jan-11 0:01 
GeneralMy vote of 4 Pin
Valentino_Lokesh17-Sep-10 7:29
memberValentino_Lokesh17-Sep-10 7:29 
GeneralMy vote of 3 Pin
chitizgaur31-Aug-10 20:15
memberchitizgaur31-Aug-10 20:15 
QuestionWhy use Delegates Pin
TheArgyleGhost7-May-09 23:01
memberTheArgyleGhost7-May-09 23:01 
AnswerRe: Why use Delegates Pin
Lee Venkatsamy13-Sep-12 11:50
memberLee Venkatsamy13-Sep-12 11:50 
This answer is a few years late, but hope this helps others.
I asked myself the same question and realized a big advantage of using delegates.
Much like the English definition of a delegate, a C# delegate basically acts as the method you pass to it upon initialization, except now you can start passing your method to other methods much like a variable. This helps a lot with common Libraries & Generic Classes.
I used delegates today in a progress form library, and the code is something like this:

In the library:

public static class ProgressThreadHelper
    #region Delegates

    public delegate void DelegatePerformProgressStep(ProgressBar progressBar, Label label, string labelText);

    public delegate void DelegateSetProgressBar(ProgressBar progressBar, Label label, int maxValue,
                                                string labelText);


    private static Form _form;
    private static Delegate _delegate;
    private static object[] _objects;
    private static Thread _processPersonThread;

    internal static object Lock = new object();

    public static void StartWork(Form form, Delegate dDelegate, params object[] pObjects)
        _form = form;
        _delegate = dDelegate;
        _objects = pObjects;


    private static void StartThreading()
        lock (Lock)
            if (_processPersonThread == null || !_processPersonThread.IsAlive)
                _processPersonThread = new Thread(() => _delegate.DynamicInvoke(_objects));
                MessageBox.Show("Still Processing this poes thing", "Be patient");

    public static void PerformProgressStep(ProgressBar progressBar, Label label, string labelText)
            new MethodInvoker(delegate
                                      label.Text = labelText;

    public static void SetProgressBar(ProgressBar progressBar, Label label, int maxValue, string labelText)
            new MethodInvoker(delegate
                                      label.Text = labelText;
                                      progressBar.Value = 1;
                                      progressBar.Maximum = maxValue;


And on the front end:
public partial class Form1 : Form
       private const string PERSON_HEADER_LABEL = "Processing Person {0} of {1}";
       private const string PERSON_DETAIL_LABEL = "Processing Account {0} of {1} for Person {2}";

       public Form1()

       private void btnProcess_Click(object sender, EventArgs e)
                                          new Action(
                                              () =>

       public void ProcessAccounts(ProgressThreadHelper.DelegateSetProgressBar setProgressBar,
                                   ProgressThreadHelper.DelegatePerformProgressStep performProgressStep)
           int noOfPeople = new Random().Next(5, 15);

           setProgressBar.Invoke(prgHeader, lblProcessHeader, noOfPeople,
                                 string.Format(PERSON_HEADER_LABEL, 0, noOfPeople));

           for (int i = 1; i <= noOfPeople; i++)
               performProgressStep.Invoke(prgHeader, lblProcessHeader,
                                          string.Format(PERSON_HEADER_LABEL, i, noOfPeople));

               int noOfAccounts = new Random().Next(1, 5);

               setProgressBar.Invoke(prgDetail, lblProcessDetail, noOfAccounts,
                                     string.Format(PERSON_DETAIL_LABEL, 0, noOfAccounts, i));

               for (int j = 1; j <= noOfAccounts; j++)
                   performProgressStep.Invoke(prgDetail, lblProcessDetail,
                                              string.Format(PERSON_DETAIL_LABEL, j, noOfAccounts, i));

                   Thread.Sleep(new Random().Next(150, 350));

               new MethodInvoker(delegate
                                         lblProcessHeader.Text = lblProcessDetail.Text = "Processed!!";

Its not the best implementation, but its something to see why delegates are useful.
Questionwebservice Pin
dhandapani8423-Aug-08 19:43
memberdhandapani8423-Aug-08 19:43 
Generalinformative... Pin
Member 31882061-Apr-08 21:47
memberMember 31882061-Apr-08 21:47 
Questiondelegates from different files Pin
oldcoastroad2-Mar-08 21:59
memberoldcoastroad2-Mar-08 21:59 
AnswerRe: delegates from different files Pin
lisan_al_ghaib23-Aug-08 23:21
memberlisan_al_ghaib23-Aug-08 23:21 
GeneralMulticast delegate Pin
Niraj_Kheria16-Sep-07 22:02
memberNiraj_Kheria16-Sep-07 22:02 
GeneralMulticast delegate [modified] Pin
Arijit Pal16-Sep-07 21:54
memberArijit Pal16-Sep-07 21:54 
GeneralV.I Question In Delegate [modified] Pin
msleem14-May-07 7:00
membermsleem14-May-07 7:00 
GeneralRe: V.I Question In Delegate Pin
Vikas_Awasthi13-Jan-11 10:01
memberVikas_Awasthi13-Jan-11 10:01 
Generallack of fanciness Pin
azam's12-Feb-07 23:45
memberazam's12-Feb-07 23:45 
GeneralSimply Great!! Pin
irula5-Jan-07 1:30
memberirula5-Jan-07 1:30 
GeneralMulticast Delegates may also call methods returning values without error Pin
ambujsaxena13-Nov-06 0:37
memberambujsaxena13-Nov-06 0:37 
GeneralRe: Multicast Delegates may also call methods returning values without error Pin
PavanPT3-Apr-07 22:27
memberPavanPT3-Apr-07 22:27 
GeneralRe: Multicast Delegates may also call methods returning values without error Pin
samir411802-Oct-07 7:04
membersamir411802-Oct-07 7:04 
GeneralErrata Pin
mohitcalling11-Oct-06 21:45
membermohitcalling11-Oct-06 21:45 
GeneralRe: Errata Pin
azam's12-Feb-07 23:21
memberazam's12-Feb-07 23:21 
GeneralRe: Errata Pin
mohitcalling18-Nov-08 21:07
membermohitcalling18-Nov-08 21:07 
GeneralRe: Errata - Why new Pin
safnate Malete3-Oct-11 18:50
membersafnate Malete3-Oct-11 18:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 15 Sep 2005


120 bookmarked