top

Search

C# Tutorial

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:Source Code: Program that demonstrates delegates in C#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);         }   } }The output of the above program is as follows:Value of Num: 17Now 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);      }
logo

C# Tutorial

Delegates in C#

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:

Source Code: Program that demonstrates delegates in C#

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);   
     }
  }
}

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);
     }

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial