When to use callbacks instead of events in c#?

后端 未结 8 1685
春和景丽
春和景丽 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: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#.

提交回复
热议问题