Click here to Skip to main content
11,925,292 members (60,592 online)
Click here to Skip to main content
Add your own
alternative version


108 bookmarked

Understanding Delegates in C#

, 16 Sep 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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 />


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.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
chitizgaur31-Aug-10 21:15
memberchitizgaur31-Aug-10 21:15 
QuestionWhy use Delegates Pin
TheArgyleGhost8-May-09 0:01
memberTheArgyleGhost8-May-09 0:01 
AnswerRe: Why use Delegates Pin
Lee Venkatsamy13-Sep-12 12:50
memberLee Venkatsamy13-Sep-12 12:50 
Questionwebservice Pin
dhandapani8423-Aug-08 20:43
memberdhandapani8423-Aug-08 20:43 
Generalinformative... Pin
Member 31882061-Apr-08 22:47
memberMember 31882061-Apr-08 22:47 
Questiondelegates from different files Pin
oldcoastroad2-Mar-08 22:59
memberoldcoastroad2-Mar-08 22:59 
AnswerRe: delegates from different files Pin
lisan_al_ghaib24-Aug-08 0:21
memberlisan_al_ghaib24-Aug-08 0:21 
GeneralMulticast delegate Pin
Niraj_Kheria16-Sep-07 23:02
memberNiraj_Kheria16-Sep-07 23:02 
GeneralMulticast delegate [modified] Pin
Arijit Pal16-Sep-07 22:54
memberArijit Pal16-Sep-07 22:54 
GeneralV.I Question In Delegate [modified] Pin
msleem14-May-07 8:00
membermsleem14-May-07 8:00 
GeneralRe: V.I Question In Delegate Pin
Vikas_Awasthi13-Jan-11 11:01
memberVikas_Awasthi13-Jan-11 11:01 
Generallack of fanciness Pin
azam's13-Feb-07 0:45
memberazam's13-Feb-07 0:45 
GeneralSimply Great!! Pin
irula5-Jan-07 2:30
memberirula5-Jan-07 2:30 
GeneralMulticast Delegates may also call methods returning values without error Pin
ambujsaxena13-Nov-06 1:37
memberambujsaxena13-Nov-06 1:37 
In this Article :

A Line is there :

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

But i have created one program with two methods which returns the double value and associate those methods with my delegate, after calling the delegate it works without any runtime exception and calls my both the functions but display only the last function returned value;

Please reply back to me the explanation of the above given problem or change the above mentioned line in the Article.

Confused | :confused:

Ambuj Saxena
Software Developer
GeneralRe: Multicast Delegates may also call methods returning values without error Pin
PavanPT3-Apr-07 23:27
memberPavanPT3-Apr-07 23:27 
GeneralRe: Multicast Delegates may also call methods returning values without error Pin
samir411802-Oct-07 8:04
membersamir411802-Oct-07 8:04 
GeneralErrata Pin
mohitcalling11-Oct-06 22:45
membermohitcalling11-Oct-06 22:45 
GeneralRe: Errata Pin
azam's13-Feb-07 0:21
memberazam's13-Feb-07 0:21 
GeneralRe: Errata Pin
mohitcalling18-Nov-08 22:07
membermohitcalling18-Nov-08 22:07 
GeneralRe: Errata - Why new Pin
safnate Malete3-Oct-11 19:50
membersafnate Malete3-Oct-11 19:50 
GeneralBest article for starters Pin
Stuti Patel1-Aug-06 20:36
memberStuti Patel1-Aug-06 20:36 
GeneralProblem in converting function pointers of VC++ to delegate in C#.Net Pin
Stuti Patel1-Aug-06 20:26
memberStuti Patel1-Aug-06 20:26 
GeneralNot bad for a first article Pin
Marc Clifton16-Sep-05 6:38
protectorMarc Clifton16-Sep-05 6:38 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 16 Sep 2005
Article Copyright 2005 by Arul Nayagam C
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid