Can I use interface method instead of delegate? How? I found searching that interface method is faster than using delegates. I would appreciate a simple code snippet.
-
1What are you talking about ? The semantics of an interface & delegate are quite different, so you whether you use A or B, depends on the situation. – Frederik Gheysels Jan 06 '10 at 10:04
-
1@Frederik: Comments with that level of abrasiveness are not welcome here. SO is a more professional place for people of all ranges of technical knowledge and ability to get help. @Samir: I agree with Mitch you've asked 25 questions its time to accept some of them. – AnthonyWJones Jan 06 '10 at 10:07
-
Adding some context to the problem would be nice. What is the concrete problem that you're trying to optimize ? Or is this a general question ? – Gishu Jan 06 '10 at 10:14
-
This is what java ppl do. You can see http://stackoverflow.com/questions/44912/java-delegates – nawfal Jul 03 '14 at 11:40
3 Answers
In theory, it's possible to accomplish everything done by delegates with interfaces (Java has no delegates, for instance) containing a single method. However, it makes the code more verbose and adds little benefit. Then again, it's possible to do everything without classes and possibly do it faster. That doesn't mean you should avoid using classes. Calling a delegate method might be marginally slower than calling an interface method. You shouldn't care about the micro-optimization. Use whatever fits your specific need.

- 414,610
- 91
- 852
- 789
Refer Delegates Are Anonymous Interfaces by Mark Seemann
In the above article Mark Seemann mentions an example
Interface Approach
public interface IMyInterface
{
int DoIt(string message);
}
public string DoStuff(IMyInterface strategy)
{
return strategy.DoIt("Ploeh").ToString();
}
Delegate Approach
public string DoStuff(Func<string, int> strategy)
{
return strategy("Ploeh").ToString();
}

- 22,196
- 67
- 260
- 418
The biggest advantage of using delegates instead of interfaces is that while a class can only provide one implementation for any given interface, it can create delegates for any number of different methods. For example, suppose rather than exposing a Click event, Button exposed a ClickRecipient property of type IButtonClickHandler, with one method "ButtonClick". A form with two buttons which should have different handlers would have to define a separate class to handle at least one of them, since the form itself would only be able to have one implementation of ButtonClick. By contrast, a Button event accepts a delegate for an EventHandler, and a form can create delegates for any number of EventHandler methods.
Note that using generics and "marker types", one could mitigate this problem significantly, by having a Button(Of T) have a ClickRecipientProperty of type IButtonClickHandler(Of T); a form could thus have a Button(Of FooButton) and a Button(Of BarButton) and provide different implementations for their interfaces. One could also have a ButtonConverter(Of T,U) wrapper class, which would implement IButtonClickHandler(Of T) and invoke IButtonClickHandler(Of U).ButtonClick.
Interfaces are good if the usage pattern will 99% of the time match what the interface is designed for (the remaining 1% of the time, one can create an intermediate wrapper class).

- 77,689
- 9
- 166
- 211