C# Delegates are Just Interfaces with one method

Dimitris Papadimitriou
2 min readAug 27, 2022

--

Functional patterns

I remember a decade ago when i was trying to understand why .net has delegates. A Delegate is just a function signature that has a Name. In this brief article i hope to help you intuitively be able to grasp that delegates are the same as interfaces with one method.

just for reference i will use the simple case of a strategy pattern. (i would use the wikipedia but is just lame). So lets say we have a OperationManager record defintion that takes an IOperation<T, R> as constructor argument and calls it inside the

the IOperation<T, R> has only one method

now we can create a dummy concrete IOperation<T, R>

public class ConcreteOperation : IOperation<int, string>
{
public async Task<string> OperationAsync(int t) => t.ToString();
}

and we can use this to instantiate an OperationManager

var manager = new OperationManager<int,string>(new ConcreteOperation());var result = manager.ExecuteActivity(1);

Replacing Interface with a delegate

Now the following delegate is equivalent to the interface

public delegate Task<R> Operation<T, R>(T t);

we can refactor the whole thing as follows.

Using Build in Delegates

if you don’t even want to define a delegate you can use the Func<T,R> and just refactor everything to this :

Summary

you can convince yourself that you can use delegate as interface in all possible situations. for example look at this extension method applied on the delegate as a decorator:

cool right, attaching behaviour to methods. C# has so many functional features.(hint:Also a tuple of delegates is equivalent to an object)

--

--