Introduction to Delegate

When we create a delegate, we are creating an object that can hold a reference to a method. Furthermore, the method can be called through this reference. Thus, a delegate can invoke the method to which it refers.

The principal advantage of a delegate is that it allows us to specify a call to a method, but the method actually invoked is determined at runtime, not at compile time.

Some Key Points:

  1. Delegates can be declared either outside a class definition or as part of a class through the use of the delegate keyword.

  2. Delegates have two parts : the delegate declaration and the delegate instance or static method.

  3. If an exception is thrown, the delegate stops processing methods in the invocation list. It does not matter whether or not an exception handler is present.

  4. The keyword delegate and the .NET infrastructure provided by the System.Delegate (all delegate types are derived) and System.Delegate.MulticastDelegate classes.

  5. Delegates are the heart  of event handling in .NET.

  6. It is a compile-time error for the same modifier to appear multiple times in a delegate declaration.

  7. Delegate types are implicitly sealed. 

(A). Simple Delegate

Declaration of delegate:

delegate-modifier delegate return-type delegate-name(parameters)

Implementation of delegate:

Delegate-name delegate-object=new Delegate-name(method of class)

Illustration with an Example:

using System;
namespace SimpleDelegate
{
    public delegate int AddNumber(int i, int j);//declaration of deletgate
    public class ImplementDelegate
    {
        public int Add(int i, int j)
        {
            return i + j;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            ImplementDelegate objimp = new ImplementDelegate();//instance of delegate
            AddNumber d = new AddNumber(objimp.Add);//implement of delegate
            sum = d(12, 13);
            Console.WriteLine("Addition of 12 and 13 is " + sum);
            Console.Read();
        }
    }
} 

Out Put:

Addition of 12 and 13 is 25

(B) MulticastDelegate
 

In simple terms, multicasting is the ability to create a chain of methods that will be called automatically when a delegate is invoked. Simply instantiate a delegate, and then use the + or += operator to add methods to the chain. To remove a method, use – or – =. 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.

Illustration with an Example:

using System;
namespace DelegateExample
{
    public delegate void MyDelegate(int i);//declaration of delegate
    public class DelegateImplement
    {

        public void ClassMethod(int i)
        {
            Console.WriteLine("Value of i in ClassMethod:{0}", i);
        }
 
        public static void StaticClassMethod(int i)
        {
            Console.WriteLine("Value of i in StaticClassMethod:{0}", i);
        }
        public void AnotherClassMethod(int i)
        {
            Console.WriteLine("Value of i in AnotherClassMethod:{0}", i);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            DelegateImplement objimp = new DelegateImplement();

            MyDelegate d = new MyDelegate(objimp.ClassMethod);//instance of delegate
            d(10);//call a delegate
            Console.WriteLine();
            d += new MyDelegate(DelegateImplement.StaticClassMethod);//Multicaste a delegate with +
            d(12);
            Console.WriteLine();
            d += new MyDelegate(objimp.AnotherClassMethod);
            d(15);
            Console.Read();
        }
    }
} 

Output:

Value of i in ClassMethod:10

Value of i in ClassMethod:12

Value of i in StaticClassmethod:12

Value of  i in ClassMethod:15

Value of i in StaticClassMethod:15

Value of  i in AnotherClassMethod:15
Ebook Download
View all
Learn
View all