When to use callbacks instead of events in c#?

后端 未结 8 1671
春和景丽
春和景丽 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:55

    In terms of OO design and class coupling there isn't great deal of difference between a callback interface and an event.

    However, I prefer events where they are things the class needs to "shout about" to whoever is interested in listening (usually multiple things) and callbacks where a specific class has requested an async operation.

    Whatever you use, use them consistently across the codebase!

提交回复
热议问题