Delegates vs. interfaces in .NET

You've almost certainly written at least one callback function. Callback functions are functions you don't call directly. They're called, using a pointer, from within some library code that you do call directly, passing the callback function's address as a parameter. The odds aren't quite so high that you've written code that takes and uses callback parameters, but the odds aren't all that low, either, as this is also a fairly common task.

Sometimes, the callback is only called once, given access to a specific part of a monstrous or opaque data structure. More commonly, your callback is called many times, in the process of selecting items from a list, or telling library code when to stop looping.

Chances are, you probably prefer to write callback code that takes strongly typed parameters. That lets you be reasonably sure a non-null parameter is a pointer to a method with the right prototype, and not a pointer to a method with a different prototype, or even a pointer to data.

When it comes to writing .NET code that takes callback parameters, your first instinct may be to use delegates, which look a lot like strongly typed method pointers in C++ or Delphi. It may not occur to you that you can implement a callback parameter as an interface instead. Interfaces and delegates share the key property of allowing you to call a method with the right prototype without knowing which object implements the method, which instance is bound to the

    Requires Free Membership to View

call, and even the name of the method you're calling.

Interface calls are faster than delegate calls. An interface reference is a reference to an instance of an object which implements the interface. An interface call is not that different from an ordinary virtual call to a method. A delegate reference, on the other hand, is a reference to a list of method pointers. While invoking a delegate looks like you're making an indirect call through a method pointer, it's actually a subroutine call that walks the list of method pointers. The overhead involved in making the call and walking the list means that delegate invocation can be two or three times slower than calling a method through an interface reference.

Interfaces are also a bit more general than are delegates. A single interface reference gives you access to all the methods of the interface. You can also check if the interface is implemented by This object type, or if the object also implements That other interface. If it does, you can cast the interface reference to an instance reference, or to a reference to another interface. Conversely, you can not go from a delegate to the instances it will call, or to any other methods those instances may support.

However, don't conclude from this that you should always implement callbacks via interfaces, not delegates. One key difference between delegates and interfaces is that you can create a delegate to any method with the right prototype.

However, you can only convert an instance reference to an interface reference if the object explicitly implements the interface. That is, while an object can "implement" an interface entirely with inherited methods, it is not enough for an object to support all the interface's methods. This means that you can't use an interface reference to callback to methods of value types or of sealed classes, unless they already support the interface.

See the conclusion of this article at Informit.com for an example of this procedure.

This was first published in October 2004

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.