Thursday, April 3, 2008

What is Event - Delegate? clear syntax for writing a event delegate

The event keyword lets you specify a delegate that will be called upon the occurrence of some "event" in your code. The delegate can have one or more associated methods that will be called when your code indicates that the event has occurred. An event in one program can be made available to other programs that target the .NET Framework Common Language Runtime.
// keyword_delegate.cs
// delegate declaration
delegate void MyDelegate(int i);
11. class Program
12. {
13. public static void Main()
14. {
15. TakesADelegate(new MyDelegate(DelegateFunction));
16. }
17. public static void TakesADelegate(MyDelegate SomeFunction)
18. {
19. SomeFunction(21);
20. }
21. public static void DelegateFunction(int i)
22. {
23. System.Console.WriteLine("Called by delegate with number: {0}.", i);
24. }
}

No comments: