Posted: 7/15/2010
Today I have learned what is delegate and events. So here I am just checking myself by this example.
Example -
using System; public delegate void DelegateUniqueNumber(int Number); class GetUniqueNumber { public event DelegateUniqueNumber UniqueNumber; public GetUniqueNumber() { UniqueNumber = null; } public void GenrateUniqueNumber() { // object of an Array class. int[] arr = new int[10]; bool flag; Random random = new Random(); for (int i = 0; i < 10; i++) { int randomNo = random.Next(1, 20); flag = true; for (int x = 0; x < arr.Length; x++) { if (randomNo == arr[x]) { i--; flag = false; break; } } if (flag) { arr[i] = randomNo; UniqueNumber(randomNo); } } } } class EventHandlerClass { public void PrintUniqueNumbers(int number) { Console.WriteLine(number); } } class MyMain { public static void Main() { GetUniqueNumber MyNumb = new GetUniqueNumber(); EventHandlerClass MyEvenNumberHandlerClass = new EventHandlerClass(); MyNumb.UniqueNumber += new DelegateUniqueNumber(MyEvenNumberHandlerClass.PrintUniqueNumbers); MyNumb.GenrateUniqueNumber(); Console.ReadLine(); } }
In above examle I am getting unique number. Whenever I get a unique number, I will fire an event and print that value.So in above example I am using delegate and event.
Hajan Can you please tell me my example is rite?
Yes :).
You have used it correctly. I didn't check in details what your GenrateUniqueNumber method actually does, but the concept you use is on the correct way.
Moreover, I would recommend you to try learning how to build components in .NET - this will definitely make you learn more advanced concepts of using delegates and events including multicasting, lambda expressions, anonymous methods and so on.
Delegates and Events play the main role especially when building components! ;)
Regards,Hajan
hajan said: Yes :).You have used it correctly. I didn't check in details what your GenrateUniqueNumber method actually does, but the concept you use is on the correct way.Moreover, I would recommend you to try learning how to build components in .NET - this will definitely make you learn more advanced concepts of using delegates and events including multicasting, lambda expressions, anonymous methods and so on.Delegates and Events play the main role especially when building components! ;)Regards,Hajan
Thank you Hajan:)
Yes I will do and let you know if I have any confusion
Feel free to ask always anything you want :) I will be glad to answer the best way I can.
hajan said: Feel free to ask always anything you want :) I will be glad to answer the best way I can.
Posted: 7/13/2011
The Event model in C# finds its roots in the event programming model that is popular in asynchronous programming. The basic foundation behind this programming model is the idea of "publisher and subscribers." In this model, you have publishers who will do some logic and publish an "event." Publishers will then send out their event only to subscribers who have subscribed to receive the specific event.
Delegates :
Delegates are very similar to 'pointers to functions' in C and C++ First we create a delegate, then we can assign any function that has a similar prototype of the one which we have specified with the delegate. Declaration: public delegate int Dlg(int x, int y); //where Dlg is the name of Delegate Create Object: when we need to create a 'Delegate object' (i.e. a pointer to a function which has a specified prototype) Dlg d1=new Dlg(function_name); //if the class is in some other class use 'class_name.function_name' function_name->has to be the name of a function which receives two integers as arguments and return an integer Invoking function: int res=d1(20, 20);
Cegonsoft