I read that you can use interfaces and delegates for the same purpose. Like, you can use delegates instead of interfaces.
Can someone provide an example? I’ve seen an example in the nutshell book but I fail to remember and wanted to ask away.
Is it possible to provide some sample code? Use case?
Thanks.
If your interface has a single method, then it is more convenient to use a delegate.
Compare the following examples:
Using an interface
Using a delegate
You can see that the delegate version is slightly smaller.
Using anonymous methods and `Func` delegates
If you combine this with built-in .NET generic delegates (
Func<T>,Action<T>and similar), and anonymous methods, you can replace this entire code with: