C# Delegates


  • Delegates are the objects which refer to a method.
  • These are like the Pointer to a function in C and C++.
  • Once a delegate refers to a method, the method can be called through that delegate.
  • The main advantage of the delegate is that it allows you to call a method but methods that are actually invoked through delegates can be determined at run time not at compile time.
  • Delegates are the objects that are inherited from the System.Delegate class.

Declaration of Delegates


  • Delegates are declared with the 'delegate' keyword.
  • The general form to declare a delegate is as follows:
  • delegate return_type name(parameter list);

  • Here 'delegate' as we just discussed is a keyword used to declare a Delegate.
  • 'return_type' is a type of value returned by the method that is referred by the Delegate.
  • 'name' is a meaningful name given to a Delegate.
  • The parameters required by the methods called through the delegate are specified in the 'parameter-list'.

Here is an example of declaration of Delegate:


delegate string Stroperation(string str);

Creating Instance of a Delegate


You can call a method by delegate through its instance. So you have to create an instance of delegate in order to call the method. Here is an example to create the instance of a delegate:


delegate string Stroperation(string str);//declaration of delegate
……….
………..
Stroperation s1=new Stroperation(ReverseString);//creating instance of delegate

In the above example ReverseString is a name of method referred by the delegate.


Here is an example which is sufficient to understand the concept of Delegate:


Example

using System;
namespace delegateexample
{
class testingdelegate
{
delegate string stroperation(string str);//declaration of a delegate
static string removespaces(string st)
{
Console.WriteLine("Remove spaces:");
string st1 = "";
int i;
for(i=0;i<(st.Length);i++)
{
if(st[i] != ' ')
{
st1=st1+st[i];
}

}
return st1;
}
static string replacespaces(string st)
{
Console.WriteLine("Replace spaces with *:");
return st.Replace(" ","*");
}
static void Main()
{
stroperation s1 = new stroperation(removespaces);//creating instance of delegate
stroperation s2 = new stroperation(replacespaces);
string str;
str = s1("C# is very interesting");//calling removespace method through delegate
Console.WriteLine(""+str); str = s2("C# is very interesting");//calling replacespaces method through delegate
Console.WriteLine("" + str);
Console.ReadLine();
}
}
}

The output of the above program will be as follows:


Output

Remove spaces:
C#isveryinteresting
Replace spaces with *:
C#*is*very*interesting

Multicast Delegates


  • One of the most exciting features of the Delegates is multicasting.
  • Multicasting is the ability to create a chain of methods that will be called automatically when a delegate is invoked.
  • A single delegate that encapsulates more than one method of same signature is known as Multicast Delegate.
  • The chain of method is very easy to create when you want to add any method to the chain you have to use '+' or'+=' operator and to remove a method from the chain you have to use '-' or '-=' operator.
  • If the delegate returns a value, then the value returned by the last method in the list becomes the return value of the entire delegate invocation. For this reason, a delegate that will make use of multicasting will often have a void return type.
  • Here is an example of Multicasting as follows:
  • Example

    namespace delegateexample
    {
    class testingdelegate
    {
    delegate void stroperation(ref string str);//declaration of a delegate
    static void removespaces(ref string st)
    {
    Console.WriteLine("Remove spaces:");
    string st1 = "";
    int i;
    for(i=0;i<(st.Length);i++)
    {
    if(st[i] != ' ')
    {
    st1=st1+st[i];
    }
    }
    st = st1;
    }
    static void replacespaces(ref string st)
    {
    Console.WriteLine("Replace spaces with *:");
    st=st.Replace(" ","*");
    }
    static void Main()
    {
    stroperation s;
    stroperation s1 = new stroperation(removespaces);//creating instance of delegate
    stroperation s2 = new stroperation(replacespaces);
    string str = "C# is very interesting";
    //creating a multicast delegate
    s = s2;
    s += s1;
    s(ref str);//calling multicast
    Console.WriteLine("Resulting String:{0}",str);
    Console.WriteLine(" ");
    Console.ReadLine();
    }
    }
    }

  • The output of the above program will be as follows:
  • Output

    Replace spaces with *:
    Remove spaces:
    Resulting String:C#*is*very*interesting