Events :

May 11, 2011
  • An event cab be defined as a special kind of delegate which facilitates the event-driven programming.
  • Event is useful in notifying objects of user interface events or state changes.
  • Class members of Events can not be called outside of the scope of the class regardless of it's access specifier.
  • An public event would allow other classes the use of += and -= on the event, but firing the event that is invoking the delegates is only allowed in the classes which contain those events.
using System.Timers; class DelegateDemo { static void Main(string[] args) { Timer t = new Timer(1000); t.Elapsed += new ElapsedEventHandler(Timer_Elapsing); t.Enabled = true; Console.WriteLine("Press enter to exit"); Console.ReadLine(); } static void Timer_Elapsing(object sender, ElapsedEventArgs e) { Console.WriteLine("1 seconde elapsed"); } }

Output:
Press enter to exit
tick
tick

Delegates :

May 11, 2011
  • Delegate is like pointer to function of C++ which is used to invoke a function based on some event.
  • Delegate is also used in Event handling to handle event based on certain condition.
  • Delegate keeps track of name of method to be called, any argument of this method, return value of this method.
  • Return type of Delegate and invoking method should be same other it will give an error and methods should be static.
  • Delegates are similar to that of C++ function pointers, but delegates are type safe.
  • Delegates allow methods to be passed as parameters.
Delegate is declared as follows: public delegate int FunctionDelegate(int x, int y);

Singlecast Delegate Example:

  • Singlecast delegate holds the reference to single methods only.
  • In this the delegate can be assigned to single method at a time.
  • They are derived from System.Delegate class
public delegate int FunctionDelegate(int x, int y); public class DelegateExample { public static int Add(int a, int b) { return a + b; } public static int divide(int a, int b) { return a / b; } } class Program { static void Main(string[] args) { FunctionDelegate del1 = new FunctionDelegate(DelegateExample.Add); int addResult = del1(6, 2);//Invoke Add() method Console.WriteLine("6 + 2 = {0}\n", addResult); FunctionDelegate del2 = new FunctionDelegate(DelegateExample.divide); int multiplyResult = del2(6, 2); Console.WriteLine("6 / 2 = {0}", multiplyResult); Console.ReadLine(); } }

Output:
6 + 2 = 8
6 / 2 = 3

Multicast Delegate Example:

  • Multicast Delegate can hold the reference to more than one methods.
  • Multicast delegates must contain only methods having return type void, else there will be a run-time exception.
public delegate void FunctionDelegate_Multi(int x, int y); public class DelegateExample { public static void Add(int a, int b) { Console.WriteLine("Called add for 6 + 2 = {0}\n", a + b); } public static void divide(int a, int b) { Console.WriteLine("Called divide for 6 / 2 = {0}\n", a / b); } } class Program { static void Main(string[] args) { FunctionDelegate_Multi del1 = new FunctionDelegate_Multi(DelegateExample.Add); del1 +=new FunctionDelegate_Multi(DelegateExample.divide); //will call both Add and Divide del1(6, 2); del1 -= new FunctionDelegate_Multi(DelegateExample.Add); //Removed Add method it will now only call divide del1(8, 4); Console.ReadLine(); } }

Anonymous Delegate:

  • You can create a delegate, but there is no need to declare the method associated with it. You do not have to explicitly define a method prior to using the delegate. Such a method is referred to as anonymous.
public delegate void Test(); public class Program { static int Main() { Test Display = delegate() { Console.WriteLine("Anonymous Delegate method"); }; Display(); return 0; } }