Click here to Skip to main content
12,302,764 members (70,581 online)
Click here to Skip to main content
Add your own
alternative version


112 bookmarked

Understanding Delegates in C#

, 15 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 5 Pin
Senthilaan22-Apr-14 23:44
memberSenthilaan22-Apr-14 23:44 
GeneralMy vote of 4 Pin
Animesh Datta10-Apr-14 23:07
memberAnimesh Datta10-Apr-14 23:07 
Generalthank you Pin
Harikrishnanvr19-Mar-14 18:48
memberHarikrishnanvr19-Mar-14 18:48 
Questiongood article Pin
nikita123deshmukh10-Feb-14 19:25
membernikita123deshmukh10-Feb-14 19:25 
GeneralNice basic demo Pin
srinivas_here10-Feb-14 8:27
membersrinivas_here10-Feb-14 8:27 
GeneralMy vote of 1 Pin
Ebram Tharwat17-Dec-13 2:16
memberEbram Tharwat17-Dec-13 2:16 
GeneralGood article... Pin
Atul Khanduri3-Dec-13 6:52
memberAtul Khanduri3-Dec-13 6:52 
QuestionCorrection to the second example Pin
John Ortiz12-Oct-13 17:19
memberJohn Ortiz12-Oct-13 17:19 
QuestionBut whats the Need? Pin
Upadhyay Praveen2-Jul-13 1:03
memberUpadhyay Praveen2-Jul-13 1:03 
AnswerRe: But whats the Need? Pin
Vishwanath Patil19-Jul-13 15:21
memberVishwanath Patil19-Jul-13 15:21 
AnswerRe: But whats the Need? Pin
Atul Khanduri3-Dec-13 6:49
memberAtul Khanduri3-Dec-13 6:49 
GeneralMy vote of 3 Pin
Jorge Altamirano27-Jun-13 14:58
memberJorge Altamirano27-Jun-13 14:58 
QuestionNice Pin
Member 26021227-Jun-13 3:11
memberMember 26021227-Jun-13 3:11 
AnswerRe: Nice Pin
Atul Khanduri3-Dec-13 10:08
memberAtul Khanduri3-Dec-13 10:08 
GeneralMy vote of 5 Pin
John Bracey6-Jun-13 10:07
groupJohn Bracey6-Jun-13 10:07 
GeneralGood one for startup.... Pin
deepakkarma13-May-13 0:14
memberdeepakkarma13-May-13 0:14 
QuestionNice Pin
KalaiselvanIT10-Apr-13 0:46
memberKalaiselvanIT10-Apr-13 0:46 
AnswerRe: Nice Pin
Atul Khanduri3-Dec-13 10:10
memberAtul Khanduri3-Dec-13 10:10 
GeneralMy vote of 3 Pin
shantanu4coding5-Apr-13 1:02
membershantanu4coding5-Apr-13 1:02 
SuggestionBasics Of Delegates Pin
rajeev tapadar25-Mar-13 6:20
memberrajeev tapadar25-Mar-13 6:20 
GeneralRe: Basics Of Delegates Pin
Atul Khanduri3-Dec-13 6:51
memberAtul Khanduri3-Dec-13 6:51 
GeneralMy vote of 5 Pin
symonsarwar21-Feb-13 5:55
membersymonsarwar21-Feb-13 5:55 
GeneralMy vote of 4 Pin
DileepMada6-Feb-13 20:20
memberDileepMada6-Feb-13 20:20 
QuestionMulticast Delegate Pin
Ranjan Kumar Yadav25-Jan-13 7:03
groupRanjan Kumar Yadav25-Jan-13 7:03 
AnswerRe: Multicast Delegate Pin
Atul Khanduri3-Dec-13 10:40
memberAtul Khanduri3-Dec-13 10:40 

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
Web01 | 2.8.160530.1 | Last Updated 16 Sep 2005
Article Copyright 2005 by Arul Nayagam C
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid