Delegates in C#

We have all learned how to implement reference types using language constructs like:

  • Classes
  • References

These reference types allow programmers to create instances of objects and use them in special ways for development procedure purposes.

Classes allow us to create objects that contain members with attributes or functionality. While on the other hand the interface allows us to declare a set of attributes and behavior that all objects are implementing for exposing them publicly or privately.

Now I am introducing a new reference type, "delegate".

A delegate is a C# language element that allows programmers to reference a method for their development work. Generally C or C++ programmers are much more familiar with the concept of delegates in comparison to others.

C# Delegates Features

  • Maximum Flexibility
  • Several built-in functionalities at runtime
  • Manipulation of variables

Delegate Features

C# Delegates Description

Delegates provide flexibility and scalability, so that it can be used consistent with the requirements or goals of your development procedure. There are fewer boundary limitations with delegates. Interfaces also can be used instead of delegates but due to some extra functionality programmers do prefer delegates instead of other reference methods.

C# Delegates Declaration

Declaration of delegates is somewhat similar to methods. Except that a delegate has the extra feature of a delegate modifier. A delegate modifier is terminated with a semicolon (;) and requires no implementation.
 
For example:

public delegate int Comparer(object obj1, object obj2)

C# Delegates Delegate Handler

A delegate handler method works as follows:

  1. Public static int CompareFirstNames(object name1, object name2);  
  2. {  
  3.    ……  
  4. }  

 

 
Reference Example

  1. using System;  
  2.    
  3. public delegate int Comparer(object obj1, object obj2);  
  4. public class Name  
  5. {  
  6.     public string FirstName = null;  
  7.     public string LastName = null;  
  8.    
  9.     public Name(string first, string last)  
  10.     {  
  11.         FirstName = first;  
  12.         LastName = last;  
  13.     }  
  14.     public static int CompareFirstNames(object name1, object name2)  
  15.     {  
  16.         string n1 = ((Name)name1).FirstName;  
  17.         string n2 = ((Name)name2).FirstName;  
  18.         if (String.Compare(n1, n2) > 0)  
  19.         {  
  20.             return 1;  
  21.         }  
  22.         else if (String.Compare(n1, n2) < 0)  
  23.         {  
  24.             return -1;  
  25.         }  
  26.         else  
  27.         {  
  28.             return 0;  
  29.         }  
  30.     }  
  31.     public override string ToString()  
  32.     {  
  33.         return FirstName + " " + LastName;  
  34.     }  
  35. }  
  36. class SimpleDelegate  
  37. {  
  38.     Name[] names = new Name[6];  
  39.     public SimpleDelegate()  
  40.     {  
  41.         names[0] = new Name("abhishek""jaiswal");  
  42.         names[1] = new Name("ashish""verma");  
  43.         names[2] = new Name("gopi""chand");  
  44.         names[3] = new Name("hari""prakash");  
  45.         names[4] = new Name("prashant""kumar");  
  46.         names[5] = new Name("pratiyush""anand");   
  47.     }  
  48.     static void Main(string[] args)  
  49.     {  
  50.         SimpleDelegate sd = new SimpleDelegate();  
  51.         Comparer cmp = new Comparer(Name.CompareFirstNames);  
  52.         Console.WriteLine("\nBefore Sorting: \n");  
  53.         sd.PrintNames();  
  54.         sd.Sort(cmp);  
  55.         Console.WriteLine("\nAfter Sorting: \n");  
  56.         sd.PrintNames();  
  57.     }  
  58.     public void Sort(Comparer compare)  
  59.     {  
  60.         object temp;  
  61.         for (int i = 0; i < names.Length; i++)  
  62.         {  
  63.             for (int j = i; j < names.Length; j++)  
  64.             {  
  65.                 if (compare(names[i], names[j]) > 0)  
  66.                 {  
  67.                     temp = names[i];  
  68.                     names[i] = names[j];  
  69.                     names[j] = (Name)temp;  
  70.                 }  
  71.             }  
  72.         }  
  73.     }  
  74.     public void PrintNames()  
  75.     {  
  76.         Console.WriteLine("Names: \n");  
  77.         foreach (Name name in names)  
  78.         {  
  79.             Console.WriteLine(name.ToString());  
  80.         }  
  81.     }  
  82. }