I must admit, I was a little confused by this question. Like @deepee, I agree that the sample code would be good here to show why you think you are using one approach over another.
The reason for my confusion is that I would not have thought to ask this question, since they serve different purposes. Interfaces are mainly used for polymorphism; so you can handle different implementations the same way.
Jon Skeet has a good example of using Func and Action.
Interfaces allow you to do this:
IAnimal animal = AnimalFactory.GetAnimal(); animal.Run();
Using the code above, you donβt know or care about which animal it is. You just know that it can work, and you want it to work. More importantly, the caller does not know how the animal works. This is the difference between Action and interfaces / polymorphism . The logic for doing something is in a particular class.
The action will allow you to do the same for each instance when the actual logic is known to the caller , instead of having each specific instance do something:
animals.ForEach(x => x.Run());
Or:
animals.ForEach(x => );
The above line of code is an action in which only the caller decides what should happen, instead of delegating the logic to the actual instance, simply calling the method on it .
They solve different problems, so I'm curious to see how people find them interchangeable in certain situations.
Bob horn
source share