10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

Delegates in C#

Updated on Sep 3, 2025
 
45,997 Views

Delegates in C# are like pointers to functions. They hold the reference of a function and this reference can be hanged at run time. Delegates are implicitly derived from System.Delegate class.

A delegate can be declared using the delegate keyword before the function signature. The syntax of a delegate is given as follows:

<access modifier> delegate <return type> <delegate name> (<parameters>)

Here the access modifier is followed by the keyword delegate. Then the return type is followed by the delegate name and the required parameters.

A program that demonstrates delegates is given as follows:

using System;
delegate void DelegateDisplay(int n);
namespace DelegateDemo
{
  class Test
  {
     public static void DisplayNum(int num)
     {
        Console.WriteLine("Value of Num: {0}", num);
     }
     static void Main(string[] args)
     {
        DelegateDisplay obj = new DelegateDisplay(DisplayNum);
        obj(17);   
     }
  }
}

Source Code: Program that demonstrates delegates in C#

The output of the above program is as follows:

Value of Num: 17

Now let us understand the above program.

The delegate is declared using the keyword delegate. The delegate name is DelegateDisplay. The code snippet for this is given as follows:

delegate void DelegateDisplay(int n);

The function DisplayNum() displays prints the value of the number num provided. The code snippet for this is given as follows:

 public static void DisplayNum(int num)
     {
        Console.WriteLine("Value of Num: {0}", num);
     }

In the function main(), the delegate DelegateDisplay is instantiated and the delegate object obj is created. The code snippet for this is given as follows:

static void Main(string[] args)
     {
        DelegateDisplay obj = new DelegateDisplay(DisplayNum);
        obj(17);
     }
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses