When to use callbacks instead of events in c#?

后端 未结 8 1647
春和景丽
春和景丽 2020-12-13 09:20

Forgive me if this a stupid question, I admit I haven\'t thought about it much.

But when would you favour using a callback (i.e, passing in a Func or Action), as oppo

相关标签:
8条回答
  • 2020-12-13 09:56

    I would use Func or Action when I am going to call the function once or use a Lambda expression.

    Events can be registered more than once which sometimes is ideal. With a callback, one has to implement a registration system for the callbacks if you want multiple.

    0 讨论(0)
  • 2020-12-13 09:57

    Well, I think they are same things. There're many different tech terms to name the same concepts or things in the different languages.

    So, what do you mean "Callback" or "event handler"?

    According to MSDN: Callback function is code within a managed application that helps an unmanaged DLL function complete a task.

    And, MADN also gives us a introduction of the difference between them.click here

    Callbacks are extensibility points that allow a framework to call back into user code through a delegate. These delegates are usually passed to the framework through a parameter of a method.

    Events are a special case of callbacks that supports convenient and consistent syntax for supplying the delegate (an event handler). In addition, Visual Studio’s statement completion and designers provide help in using event-based APIs

    Also, in some books, such as this book, the author seemed say the same thing with MSDN.

    Therefore, in my opinion, you can't say use callbacks instead of events in the C#.

    0 讨论(0)
提交回复
热议问题