When should we use event in C#

こ雲淡風輕ζ 提交于 2019-12-08 06:02:09

问题


I am a newbie of C# and WPF. And I just learned the asynchronous programming in C#. We raise event and some where we catch this event.

The advantage of this method is we don't need to call directly the object method. Example we load data from database, then after load data method is finish, it raise the event LoadDataSuccessfully Then the main class catch this event and raise event UpdateGUIAfterLoadDataSuccessfully, some other Control will catch this event and update GUI.

But now I think may be... I use it too much in my project, may be i overuse it too much. Please give me some advices, when we should use Event and when we should do the traditional way.


回答1:


Events should be used when it is inappropriate for the code which originates the action to have direct knowledge of the code(s) which react to that action.

Lets explain with an help of example

Question

In your System you have Data objects sent to you say you have 50 Data object sent per minute from an external system.Upon receiving this you need to have it processed by another object.

Would an event or a simple method call be better to use in this situation?

Answer

On one hand, an event does sound appropriate here, because the code which handles data reception should not be dependent on the implementation of the code which does something with said data. Otherwise the data reception code is now responsible for two things - receiving the data and delegating the processing of it.

On the other hand, if the particular processing of the data is directly tied to act of it being sent by the external caller, it may make more sense to make it a function call

Take a look at this




回答2:


Here are couple comments/answers to what you wrote above

“The advantage of this method is we don't need to call directly the object method.” – true but this doesn’t mean you should events for everything and avoide explicit method calling at all. Example Kyle provided is a good one. Also – look at how events are used in .NET by default – they use if to asynchronous events only (mouse click, mouse over, and such)

“Example we load data from database, then after load data method is finish, it raise the event LoadDataSuccessfully Then the main class catch this event and raise event UpdateGUIAfterLoadDataSuccessfully, some other Control will catch this event and update GUI.” – I’d only go with this method if data needs to be refreshed from time to time but not even sure about that. It’s quite common not to use events for loading data in a way you described here. Note that what you wrote implies that you have one thread for loading data and another one for GUI…

“I use it too much in my project, may be i overuse it too much.” – you are probably correct on this.




回答3:


In short 'Event' are use to let the other objects decide what actions or code should be executed on event triggers, but I would recommend to start using 'Commands' for more information http://msdn.microsoft.com/en-IN/library/ms752308.aspx



来源:https://stackoverflow.com/questions/16139649/when-should-we-use-event-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!