Delegates in C#

Introduction

A delegate is an object that can refer to a method. Therefore when you create a delegate, you are creating an object that can hold a reference to a method. The method can be called using this reference. In other words, a delegate can invoke the method and is considered to be a powerful feature.

Advantage

The same delegate can be used to call various methods during runtime of a program by simply changing the method. Therefore, thay is an advantage of a delegate.

Declaration

  • A delegate type is declared by the keyword delegate.
  • Delegate return-type name (parameter list);
  • Where return-type defines the type of value returned by the methods.

Example

  1. using System;  
  2. namespace ConsoleApplication3  
  3. {  
  4.     delegate string delegate_method(string str);  
  5.     class delegate_test  
  6.     {  
  7.         static string Reverse(string s)  
  8.         {  
  9.             string temp = "";  
  10.             int i, j;  
  11.             Console.WriteLine("Reversing String...");  
  12.             for (j = 0, i = s.Length - 1; i >= 0; i--, j++)  
  13.                 temp += s[i];  
  14.             return temp;  
  15.         }  
  16.         static void Main(string[] args)  
  17.         {  
  18.             delegate_method del = new delegate_method(Reverse);  
  19.             string str;  
  20.             del = new delegate_method(Reverse);  
  21.             str = del("neeraj kumar");  
  22.             Console.WriteLine("Result is: "+str);  
  23.             Console.Read();  
  24.         }  
  25.     }  
  26. }  
Output 

    Reversing String…
    Result is: ramuk jareen

output

The del object is assigned Reverse() then del is called. Then Reverse() is being called.

Delegate method group conversion

A delegate method group conversion defines the simple way of using a delegate. It simplifies the syntax that assigns a method to a delegate. It is called method group conversion.

  1. static void Main(string[] args)  
  2. {  
  3.     delegate_method del = Reverse;//use method group conversion  
  4.     string str;  
  5.     del = new delegate_method(Reverse);  
  6.     str = del("neeraj kumar");  
  7.     Console.WriteLine("Result is: "+str);  
  8.     Console.Read();  
  9. }  
Multicasting delegates

Delegates also support an exciting feature called multicasting. Multicasting is the ability to create a chain of methods that will be automatically called when a delegate is invoked and is easy to create. We use the + or the += operator to add methods to the chain and to remove the methods we use – or -=.

Example
  1. using System;  
  2. namespace ConsoleApplication3  
  3. {  
  4.     delegate void delegate_method(ref string str);  
  5.     class delegate_test  
  6.     {  
  7.         static void Reverse(ref string s)  
  8.         {  
  9.             string temp = "";  
  10.             int i, j;  
  11.             Console.WriteLine("Reversing String...");  
  12.             for (j = 0, i = s.Length - 1; i >= 0; i--, j++)  
  13.                 temp += s[i];  
  14.             s = temp;  
  15.         }  
  16.         static void Uppercase(ref string s)  
  17.         {  
  18.             Console.WriteLine("Converting into uppercase...");  
  19.             s = s.ToUpper();  
  20.               
  21.         }  
  22.         static void Main(string[] args)  
  23.         {  
  24.             delegate_method del;  
  25.             delegate_method upper = Uppercase;  
  26.             delegate_method reverse = Reverse;  
  27.             string str="neeraj kumar";  
  28.             // setup multicasting  
  29.             del = upper;  
  30.             del += reverse;  
  31.             //call multicasting  
  32.             del(ref str);  
  33.             Console.WriteLine("Result is: "+str);  
  34.             Console.Read();  
  35.         }  
  36.     }  
  37. }  
Output

    Converting into uppercase…
    Reversing String…
    Result is: RAMUK JAREEN

I hope this article will help you understand delegates.

Up Next
    Ebook Download
    View all
    Learn
    View all