Delegates in C# Language

C# delegates are similar to pointers to functions in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.

When you want to create a delegate in C# you use the delegate type.

A Delegate is a type-safe object that can point to another method or possibly multiple methods in the application that can be invoked at a later time. Declaring a delegate is just like declaring a method, but it works as a Class type.

Syntax for delegate declaration:

delegate <return type> <delegate-name> <parameter list>

See in the figure:

multi

Delegates have the capability to invoke more than one method at the same time.

Delegate are especially used for implementing events and call-back methods. All delegates are implicitly derived from System. Delegates class System. delegates is an abstract class. It's an OOP feature that we cannot create an instance of an abstract class. Therefore to make an instance of system.delegates we need to inherit it from a class.

See in the figure:

delegate

Instantiating Delegates

A delegate object must be created with the new keyword and associated with a specific method. When creating delegates, the argument passed to the new expression is written like a method call.

The following example describe the declaration, instantiation and use of a delegate:

  1. using System;  
  2.   
  3.   
  4. namespace delegatetest  
  5. {  
  6.     //delegate void stringprint(string str);  // create a delegate type (inherited from system.delegate class)  
  7.     class Program  
  8.     {  
  9.         delegate void stringprint(string str);    
  10.         public static void stringmethod(string s)  
  11.         {  
  12.             Console.WriteLine("heloo stringmethod");  
  13.           
  14.         }  
  15.   
  16.         static void Main(string[] args)  
  17.         {  
  18.             stringprint printer = new stringprint(stringmethod);  //create a delegates instance  
  19.             printer(" helo word");                                //invoke the delegates  
  20.             Console.ReadKey();  
  21.         }  
  22.   
  23.     }  
  24. }  

Console Output

output

Note

We can declare a delegate type inside or outside the class because a delegate is a class type.

In the preceding example, I just declared a delegate type inside the class.

The following example describes the declaration, instantiation and use of a delegate that can be used to reference a method that takes an integer parameter and returns an integer value.

  1. using System;  
  2. namespace delegatsretun  
  3. {  
  4.    delegate int manipulatenumber(int n); //create a delegate type (inherited from system.delegate class)  
  5.   
  6.    class Program  
  7.    {  
  8.       static int num = 10;  
  9.   
  10.       public static int addnumber(int p)  
  11.       {  
  12.   
  13.          num = num + p;  
  14.          return num;  
  15.   
  16.       }  
  17.       public static int mult(int q)  
  18.       {  
  19.          num = num * q;  
  20.          return num;  
  21.       }  
  22.   
  23.       public static int getresult()  
  24.       {  
  25.          return num;  
  26.   
  27.       }  
  28.       static void Main(string[] args)  
  29.       {  
  30.          Console.WriteLine("enter two values for add and multiple");  
  31.          int l =Convert .ToInt32( Console.ReadLine());   
  32.          int x = Convert.ToInt32(Console.ReadLine());  
  33.          manipulatenumber mp = new manipulatenumber(addnumber); //create a delegates instance  
  34.          mp(l); //invoke the delegates  
  35.          Console.WriteLine("value of addnumber {0}", getresult());  
  36.          manipulatenumber mps = new manipulatenumber(mult); //create a delegates instance  
  37.          mps(x); //invoke the delegates  
  38.          Console.WriteLine("value of mult {0}", getresult());  
  39.          Console.ReadKey();  
  40.       }  
  41.    }  
  42. }  

Console Output

console output

Multicasting of a Delegate

Multicast delegates can be used to invoke multiple methods. The delegates instance can do multicasting and add a new method on the existing delegates instance using the "+=" operator and "-="operators can be used to add and remove (respectively) a method from a delegates instance. The following program describes the multicasting of delegates.

  1. using System;  
  2.   
  3. namespace multicast  
  4. {  
  5.     delegate int manipulatenumber(int n);//create a delegate type (inherited from system.delegate class)  
  6.   
  7.   
  8.     class Program  
  9.     {  
  10.         static int num = 10;  
  11.   
  12.         public static int addnumber(int p)  
  13.         {  
  14.   
  15.             num = num + p;  
  16.             return num;  
  17.   
  18.         }  
  19.         public static int mult(int q)  
  20.         {  
  21.             num = num * q;  
  22.             return num;  
  23.         }  
  24.   
  25.         public static int getresult()  
  26.         {  
  27.             return num;  
  28.   
  29.         }  
  30.         static void Main(string[] args)  
  31.         {       // create delegate instance  
  32.             manipulatenumber md;  
  33.             manipulatenumber md1 = new manipulatenumber(addnumber);  
  34.             manipulatenumber md2 = new manipulatenumber(mult);  
  35.             md = md1;  
  36.             md += md2;  
  37.             //calling  multicasting  
  38.             md(20);  
  39.   
  40.             Console.WriteLine("value of num {0}", getresult());  
  41.             Console.ReadKey();  
  42.   
  43.   
  44.         }  
  45.     }  
  46. }  

Console Output

outputmulti

Summary 

In this article we came across what the basics of delegates actually are. I tried to put light on them with examples.


Similar Articles