C#: Callback Patterns and Choice

Callback Patterns and Choice
Use Event if the following are true:
– One signs up for the callback up front (typically through Add and Remove methods).
– More than one object will typically be cared.
– One is designing a high-level API, esp. a designable Component.
Use Delegate if the following are tru:
– You want a C style function pointer.
– Single callback.
– Registered in a call or at a construction time (not through separate Add method).
Use Interface if the following is true.
– The callback entails complex behavior.
– Do favor using events over methods that take delegates. Events are a more familiar idea to developers, and therefore are simpler to use. Methods taking delegates should only be used when the target user fully understands the concept of delegates, and the situation is not event-related.
Situations such as notification of an object’s state change should be implemented by events only.
Tradeoff: Events are definitely not appropriate as a general purpose customization mechanism. They have much higher performance overhead and as not as flexible as, for example, inheritance based customization.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s