Delegate
January 26, 2012 Leave a comment
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 Features:
- Delegates can be declared either outside a class definition or as part of a class through the use of the delegate keyword.
- Delegates have two parts in the relationship: the delegate declaration and the delegate instance or static method.
- 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.
- The keyword delegate and the .NET infrastructure provided by the System.Delegate (all delegate types are derived) and System.Delegate.MulticastDelegate classes.
- Delegates are the heart and soul of event handling in .NET.
- It is a compile-time error for the same modifier to appear multiple times in a delegate declaration.
- 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); 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(); AddNumber d = new AddNumber(objimp.Add); sum=d(12, 13); Console.WriteLine("Addition of 12 and 13 is " + sum); Console.Read(); } } }
Out Put:
(B) MulticastDelegate
One of the most exciting features of a delegate is its support for multicasting. 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); 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); d(10); Console.WriteLine(); d += new MyDelegate(DelegateImplement.StaticClassMethod); d(12); Console.WriteLine(); d += new MyDelegate(objimp.AnotherClassMethod); d(15); Console.Read(); } } }