Simple Delegate Example


/ Published in: C#
Save to your folder(s)



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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.