HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

Delegates in C# : Part 1
Definition:
Delegates in C# are type safe function pointers that can hold a reference to one or more methods that have the same signature as that of a delegate. Delegates are similar to functions pointers in C/C++.

Let us understand delegates with a simple example. In the example below
  • We have declared a SampleDelegate using the delegate keyword. The SampleDelegate's return type is void and takes a single parameter of type string.
  • In the Main() method we instantiate the SampleDelegate and we pass the address of the Display() method to the constructor of the SampleDelegate. We specify the Display() method without the paranthesis which means that we are passing the reference of the Display() method to the SampleDelegate.
  • Delegate signature should match the signature of the method, the delegate is refering to. In our example the signature of the SampleDelegate and the Display() method are same. This is why delegates are said to be type safe.
  • We finally invoke the SampleDelegate. As the SampleDelegate holds a pointer to the Display() method, the method will be invoked automatically. As delegates holds a pointer(address) to function, they are called as function pointers.

using System;

//Delegate declaration

public delegate void SampleDelegate(string Text);

class DelegatesDemo

{

    public static void Display(string Text)

    {

        Console.WriteLine("The string you entered is : " + Text);

    }

    public static void Main()

    {

        //Instantiate the delegate and pass the reference of the

        //Display() method.

        SampleDelegate S = new SampleDelegate(Display);

        Console.WriteLine("Enter your text to display");

        string Text = Console.ReadLine();

        //Invoke the delegate, which inturn will invoke the Display() method

        S(Text);

    }

}

Types of Delegates
In general we have 2 types of delegates.
  • Single Cast Delegates : Delegates which hold a reference to a single method are called as Single Cast delegates.
  • Multi   Cast Delegates : Delegates which hold reference to more than one method are called as multi Cast delegates.
Example for Multi Cast Delegate
In the example below
  • We have created 2 instances of SampleDelegate S1 and S2.
  • S1 holds a reference to FirstMethod() and S2 holds a reference to the SecondMethod().
  • S1+=S2; This statement will make S1 to point to both FirstMethod() as well as SecondMethod();
  • Finally we invoke S1() which inturn will invoke both FirstMethod() and SecondMethod()

using System;

public delegate void SampleDelegate();

class DelegatesDemo

{

    public static void FirstMethod()

    {

        Console.WriteLine("FirstMethod() Invoked by Delegate");

    }

    public static void SecondMethod()

    {

        Console.WriteLine("SecondMethod() Invoked by Delegate");

    }

    public static void Main()

    {

        SampleDelegate S1 = new SampleDelegate(FirstMethod);

        SampleDelegate S2 = new SampleDelegate(SecondMethod);

        S1 += S2;

        S1();

    }

}

Further Reading

Njoy Programming
   ByPrasad Cherukuri