Raising events vs direct method calls differences

前端 未结 6 2176
夕颜
夕颜 2021-02-04 11:27

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 mechan

6条回答
  •  忘掉有多难
    2021-02-04 12:23

    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.

提交回复
热议问题