none
Difference BT "invoking a method using delegates when an event arises" vs. "the old fashioned way of "directly calling the method when a specific condition is met" C-sharp RRS feed

  • Question

  • Hi,

    C -Sharp gives programmers the ability to write Custom Events, which when risen invoke the event handler refernced by a delegate.

    Now we can also call a method or methods directly, in the conventional fashion when a particular event occurs. So I do not understand why one would use the delegate method mentioned above. What does it buy one to use delegates to invoke methods when events arise. Why not call the needed method directly when a specific condition is met (for ex. bank account falls below a threshold). Is the event handler which gets called using the delegate method, called asynchronously, is that reason ?

    If anybody can explain the difference to me i will appreciate..

    Thank you

     

    Wednesday, May 26, 2010 4:43 AM

Answers

  • Hi Diana4,

        The .NET event infrastructure provides a mechanism allowing software components to send notifications to each other. With event and delegate, we defined the protocal of the notification a software component capable of sending and other software components could consume.

        As you asked, why not just invoke some target method when certain condition is met? Let's call the software component which sends the event as the event sender, and the software component whose method is called to handle the event the event consumer. Think it this way, if things goes as you imaged, the sender and the consumer will be tightly coupled.  But the fact is the sender and consumer may not be written by the same company, it's quite often that the sender will never know the actual name of method it can call. All the sender can do is to tell the outside world what type of method it wants to invoke, that is the point of protocal of notification, which is representd by a delegate.

       A delegate is just like a type-safe version of function pointer if you are familiar with C++. But it provides much more than a simple function pointer. Please refer to here for more details.

       Hope this could help you.


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Thursday, May 27, 2010 9:43 AM refine
    • Marked as answer by Diana4 Thursday, May 27, 2010 2:54 PM
    Thursday, May 27, 2010 9:40 AM

All replies

  • Hi Diana4,

    Delegat is lika a pointer to a function. Actually, it's a pointer. Suppose we want to call one of more than one function simultaniously then we have to delegate. We have to define the function in the delegate and now if we cll the delegate then the functions will automatically called. But to call that function we have to keep in mind that "the parameter type, retun type and number of parameters will be same"

    As you said, when an event is arised, you can just call a function to handle it, but if you want the event can be handled by lots of functions, what will you do then? Call the functions one by one when you fire the event? That's rediculous. However, you can add the handling functions to only one delegate, then when the event is fired, just one delegate will be called. Doesn't it sound good?

    Of course, what I said above is just my own opinion.


    Hope this helpful to you! If you have any further quetions, please feel free to let me know.
    Please mark the right answer at right time.
    Bset Regards,
    Tracy
    Thursday, May 27, 2010 2:12 AM
  • Hi Diana4,

        The .NET event infrastructure provides a mechanism allowing software components to send notifications to each other. With event and delegate, we defined the protocal of the notification a software component capable of sending and other software components could consume.

        As you asked, why not just invoke some target method when certain condition is met? Let's call the software component which sends the event as the event sender, and the software component whose method is called to handle the event the event consumer. Think it this way, if things goes as you imaged, the sender and the consumer will be tightly coupled.  But the fact is the sender and consumer may not be written by the same company, it's quite often that the sender will never know the actual name of method it can call. All the sender can do is to tell the outside world what type of method it wants to invoke, that is the point of protocal of notification, which is representd by a delegate.

       A delegate is just like a type-safe version of function pointer if you are familiar with C++. But it provides much more than a simple function pointer. Please refer to here for more details.

       Hope this could help you.


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Thursday, May 27, 2010 9:43 AM refine
    • Marked as answer by Diana4 Thursday, May 27, 2010 2:54 PM
    Thursday, May 27, 2010 9:40 AM
  • Thank you
    Thursday, May 27, 2010 2:54 PM