Using Interfaces Against Func or Action

I have been writing C # code for 10 years, but I'm terribly weak knowing when to use the interface or use Func or Action. It seems to me that in many places where a method is called on an interface, Func or Action will work just as well. So, I think my question is this. If I have an interface with only one method, or possibly several methods, is there a drawback to using Func or Action instead? Using Func or Action seems cleaner to me.

Many thanks.

+7
source share
4 answers

I think you can compare Action or Func with an interface containing one method, with the difference that you can provide any Action or Func that meets the requirements for the parameter / return, where when using interfaces, the supplied object must implement this interface.

Perhaps you could call Action and Func "anonymous interfaces of one method."

If you look at the perspective of design, your class model will draw blocks without any lines between them.

+6
source

You should use delegates and lambda expressions if you expect the implementations to be very short (one or two lines), and especially if the implementations require local variables (closures).

+3
source

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 => /* do something completely different here */); 

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.

+2
source

You would use an interface when you don't care which object you work with ...

Release the sample tutorial

open class Animal;

 public class Dog : Animal, IRunningAnimal { } public class Cheetah : Animal, IRunningAnimal { } public class Fish : Animal, ISwimmingAnimal { } public class Gator : Animal, ISwimmingAnimal, IRunningAnimal { } public interface IRunningAnimal { public void Run(); } public interface ISwimmingAnimal { public void Swim(); } public abstract class Animal { /// ... public abstract void Move(); } 

somewhere in the code ...

 RunningAnimal runner = getAnimal(); //make him run runner.Run(); 

every running animal can work differently, but they all can work.

or better

 if(getAnimal() instanceof RunningAnimal) getAnimal().Run(); else getAnimal().Move(); 
+1
source

All Articles