Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raising events vs direct method calls differences

Tags:

c#

Raising an event, will call its event handler. eg http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx

What is the difference between using the events mechanism and direct calls to other methods (eg if a condition is met in method A(), call B() )?

And what is the difference between consuming and raising events?

Thanks

like image 818
GurdeepS Avatar asked Apr 18 '10 00:04

GurdeepS


People also ask

What is the difference between methods and events?

Both are different. A method is nothing but a function which executes something in it when called. it can be called any time. A event is a result of a action performed by the user like click, hover, drag, re-size etc.

What happens if an event occurs and there is no event handler to respond to the event?

TF: if an event occurs and there is not event handler to respond to that event, the event ins ignored.

Which is the best place to subscribe event handler to an event?

To subscribe to events by using the Visual Studio IDEOn top of the Properties window, click the Events icon. Double-click the event that you want to create, for example the Load event. Visual C# creates an empty event handler method and adds it to your code. Alternatively you can add the code manually in Code view.

What is delegate and event in C# with example?

A delegate is a way of telling C# which method to call when an event is triggered. For example, if you click a Button on a form, the program would call a specific method. It is this pointer that is a delegate. Delegates are good, as you can notify several methods that an event has occurred, if you wish so.


4 Answers

The difference is this:

Method call = "Do this specific thing"

Event raise = "If anyone is listening and cares, this thing just happened."

It is central to Separation of Concerns and reusability. A button is not a reusable component if clicking it calls a specific method. But if it simply "announces" to the program it was clicked, and interested parties are responsible for subscribing themselves to that, it is infinitely reusable.

The underlying technical implementation of how this is accomplished (via delegate) is irrelevant.

like image 189
Rex M Avatar answered Oct 28 '22 03:10

Rex M


Raising an event, will call its event handler

That started off wrong. There could be no event handler. Or many. You don't know. Which is the major difference from calling a method directly. Look up "observer pattern" in your favorite design patterns book.

like image 36
Hans Passant Avatar answered Oct 28 '22 03:10

Hans Passant


What is the difference between using the events mechanism and direct calls to other methods (eg if a condition is met in method A(), call B() )?

Business logic wise there is no difference between the two. What I mean by this is that you can accomplish the same task each way. It's just a different way of going about it. The real difference is the amount of work you have to do to handle the notification of other modules.

With raising an event, you are essentially saying "Hey, something has occurred any piece of code which has signed up to be notified when this happened, let them know. Which modules that get notified is not my concern, because I am assuming that (at runtime) all modules that need to know are setup for notification."

With calling each method directly, you are making the decision that you are going to tell this (or these) modules, and only these, that something has occurred. You are making that assertion that no matter what the states of these modules are in isn't important and they need to know this event happened.

Both are correct for different situations. Event notifications are more dynamic. Different modules can register and de-register for notifications. Direct method calls are more static. A certain set of objects (or modules etc.) are absolutely going to be notified (barring exceptions of course) that something happened, but only these are going to be notified.

like image 3
kemiller2002 Avatar answered Oct 28 '22 02:10

kemiller2002


Raising an event (or Invoking, to use the term from your link) means you are sending the event to all consumers. For example, a window can raise an event when it is clicked with the mouse.

Consuming an event means that you are receiving and processing the event from whoever sent it. For example, you might want to know when the window is clicked by the mouse.

If you have only one consumer, then you could accomplish something similar by just supplying a callback directly:

// 'Event' type:
delegate void DelMyEvent();
// consumer:
class Consumer
{
    Producer _theProducer;
    void RegisterForNotification()
    {
       _theProducer.OnMyEvent = new DelMyEvent(OnMyEvent);
    }
    void OnMyEvent() { }
}
// producer:
class Producer
{
   public DelMyEvent OnMyEvent;
   void SendNotification()
   {
      if( OnMyEvent != null ) OnMyEvent();
   }
}

The event mechanism cleans this up a bit by preventing the consumer from setting the delegate value directly. Instead it makes the consumer register itself with the += operator. When the first consumer registers, the delegate gets set, and when the second consumer registers, their two callbacks get chained together by Delegate.Combine.

like image 2
Eric Avatar answered Oct 28 '22 03:10

Eric