Syntax of delegate

  1. Delegate [ data-type ] [ delegate-name] [argument-list]

Following example demonstrates about Multicasting of a delegate with Arithmatic operations.When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type.

Let see the example:

  1. using System;
  2. namespace Delegate_Operation
  3. {
  4. delegate int Delegate(int x, int y);
  5. class Delegate_Exmaple
  6. {
  7. static Delegate dobj = Add;
  8. static int Add(int a, int b)
  9. {
  10. return a + b;
  11. }
  12. static int Sub(int a, int b)
  13. {
  14. return a - b;
  15. }
  16. static int Mul(int a, int b)
  17. {
  18. return a * b;
  19. }
  20. static int Div(int a, int b)
  21. {
  22. return a / b;
  23. }
  24. static void Main(String[] args)
  25. {
  26. int call;
  27. call = dobj.Invoke(5, 5);
  28. Console.WriteLine("The Sum of {0} + {1} is : {2}",5,5,call);
  29. Console.WriteLine();
  30. dobj += new Delegate(Sub);
  31. call = dobj.Invoke(5, 5);
  32. Console.WriteLine("The Difference of {0} - {1} is : {2}", 5, 5, call);
  33. Console.WriteLine();
  34. dobj += new Delegate(Mul);
  35. call = dobj.Invoke(5, 5);
  36. Console.WriteLine("The Multiplication of {0} * {1} is : {2}", 5, 5, call);
  37. Console.WriteLine();
  38. dobj += new Delegate(Div);
  39. call = dobj.Invoke(5, 5);
  40. Console.WriteLine("The Divition of {0} / {1} is : {2}", 5, 5, call);
  41. Console.ReadKey();
  42. }
  43. }
  44. }

Now deploy this code,so you will get following result

Hope, this example will help you..! Cheers....