When to use callbacks instead of events in c#?

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

    I use callbacks in a few cases where I know it will only ever fire once, and the callback is specific to a single method call (rather than to an object instance) - for example, as the return part of an async method.

    This is particularly true of static utility methods (since you don't have an instance, and static events are deadly when used carelessly, and to be avoided), but of course the other option is to create a class instance with an event instead.

提交回复
热议问题