52 out of 89 rated this helpful - Rate this topic

When to Use Delegates Instead of Interfaces (C# Programming Guide)

Both delegates and interfaces allow a class designer to separate type declarations and implementation. A given interface can be inherited and implemented by any class or struct; a delegate can created for a method on any class, as long as the method fits the method signature for the delegate. An interface reference or a delegate can be used by an object with no knowledge of the class that implements the interface or delegate method. Given these similarities, when should a class designer use a delegate and when should they use an interface?

Use a delegate when:

  • An eventing design pattern is used.

  • It is desirable to encapsulate a static method.

  • The caller has no need access other properties, methods, or interfaces on the object implementing the method.

  • Easy composition is desired.

  • A class may need more than one implementation of the method.

Use an interface when:

  • There are a group of related methods that may be called.

  • A class only needs one implementation of the method.

  • The class using the interface will want to cast that interface to other interface or class types.

  • The method being implemented is linked to the type or identity of the class: for example, comparison methods.

One good example of using a single-method interface instead of a delegate is IComparable or IComparable. IComparable declares the CompareTo method, which returns an integer specifying a less than, equal to, or greater than relationship between two objects of the same type. IComparable can be used as the basis of a sort algorithm, and while using a delegate comparison method as the basis of a sort algorithm would be valid, it is not ideal. Because the ability to compare belongs to the class, and the comparison algorithm doesn’t change at run-time, a single-method interface is ideal.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Better Information on Delegates Usage in Threading and Dynamic Binding of Controls with their Events
$0Delegate's word meaning is representative. A delegate in .Net is juts like a function pointer in C#. $0 $0The most important property of a function pointer is that the syntax of the function and the pointer to the function must exactly be same. The returntypes and the list of arguments must match exactly. $0 $0 $0Eg:$0 $0 $0If a function looks like: int fact(int)$0 $0 $0Then its function pointer should look like:     int *p (int)$0 $0Here, as you may see the return types and the list of arguments of both function and function pointer match exactly.$0 $0 $0When we use delegate then there are 3 stages which we need to follow:$0 $0 $0Declaration: delegate <returntype> delegateName (<list of arguments>)$0 $0 $0Object Creation: delegateName objDelegate = new delegateName(<function to which the delegate points>)$0 $0Invoking: objDelegate(<list of arguments that are to be passed to the function>)$0 $0The main uses of delegate comes in the following circumstances:$0 $0 $01) When we want to create the controls dynamically, then we cannot create the events for these controls beforehand as these controls are created only on runtime. In that case, if we want to bind these controls with their respective events then we need to use delegate. $0 $0 $0 $02) Delegate is also used in multitasking using threading.$0 $0 $0 $0A detailed video tutorial has been provided on Delegate's usage for dynamic binding of controls with their events at the below link:$0 $0$0 $0 $0http://visiontechno.net/studymats/delegates.html$0 $0
typo
The caller has no need [to] access other properties, methods, or interfaces on the object implementing the method.
+to


Edit by SJ at MSFT: That typo (which I almost always enter as "type") was caught and corrected in Visual Studio 2008 (http://msdn.microsoft.com/en-us/library/ms173173(VS.90).aspx). Thanks for your input.
"IComparable or IComparable"?!
The text reads "One good example of using a single-method interface instead of a delegate is IComparable or IComparable." The two identical words are links, the first to the non-generic, the second to the generic, but use of parenthetical information to indicate that would be advisable. The paragraph is not very readable.


CS Team: This is fixed in later versions of the documentation. Thanks.
typo

The line " a delegate can created for a method on any class" must be read as " a delegate can be created for a method on any class"


CSTeam: This has been fixed in later versions of the documentation. Thanks.

typo
The line "The caller has no need access other..." is missing a "to" or something...


CSTeam: This sentence is fixed in later versions of the documentation. Thanks.
Advertisement