Thursday 8 July, 2010

SIMPLE DELEGATE EXAMPLE

  1. using System;
  2. namespace DelegateTest
  3. {
  4. public delegate void TestDelegate(string message);
  5. class Program
  6. {
  7. public static void Display(string message)
  8. {
  9. Console.WriteLine("");
  10. Console.WriteLine("The string entered is : " + message);
  11. }
  12. static void Main(string[] args)
  13. {
  14. //-- Instantiate the delegate
  15. TestDelegate t = new TestDelegate(Display);
  16. //-- Input some text
  17. Console.WriteLine("Please enter a string:");
  18. string message = Console.ReadLine();
  19. //-- Invoke the delegate
  20. t(message);
  21. Console.ReadLine();
  22. }
  23. }
  24. }

No comments:

Post a Comment