A simple way of thinking about Action<>
Most of us are pretty familiar with finding sections of repeated code, pulling that code out into a method and making that method take parameters to represent the differences.
Here is a small example, which should look pretty familiar:
01 | public void SteamGreenBeans() |
03 | var greenBeans = new GreenBeans(); |
05 | Steam(greenBeans, Minutes.Is(10)); |
09 | public void SteamCorn() |
11 | var corn = new Corn(); |
13 | Steam(corn, Minutes.Is(15)); |
17 | public void SteamSpinach() |
19 | var spinach = new Spinach(); |
21 | SteamVegetable(spinach, Minutes.Is(8)); |
Each one of these methods pretty much does the same thing. The only difference here is the type of vegetable and the time to steam it.
It is a simple and common refactor to refactor that code to:
01 | public void SteamGreenBeans() |
03 | SteamVegetable(new GreenBeans(), 10); |
06 | public void SteamCorn() |
08 | SteamVegetable(new Corn(), 15); |
11 | public void SteamSpinach() |
13 | SteamVegetable(new Spinach(), 8); |
16 | public void SteamVegetable(Vegetable vegetable, int timeInMinutes) |
19 | Steam(vegetable, Minutes.Is(timeInMinutes)); |
Much better, now we aren’t repeating the “actions” in 3 different methods.
Now let’s imagine we want to do something more than steam. We need to be able to fry or bake the vegetables. How can we do that?
Probably we will have to add some new methods for doing that. So we will end up with something like this:
01 | public void SteamVegetable(Vegetable vegetable, int timeInMinutes) |
04 | Steam(vegetable, Minutes.Is(timeInMinutes)); |
08 | public void FryVegetable(Vegetable vegetable, int timeInMinutes) |
11 | Fry(vegetable, Minutes.Is(timeInMinutes)); |
15 | public void BakeVegetable(Vegetable vegetable, int timeInMinutes) |
18 | Bake(vegetable, Minutes.Is(timeInMinutes)); |
Hmm, lots of duplication again. No problem. Lets just do what we did to the first set of methods and make a CookVegetable method. Since we always clean, then cook, then serve, we should be able to just pass in the method of cooking we will use.
Oh wait, how do we do that? We can’t just extract out Bake or Fry or Steam, because the Bake, Fry and Steam methods are logic and not data.
Unless… unless we can make them data. Can we do that?
We sure can, check this out:
01 | public void SteamVegetable(Vegetable vegetable, int timeInMinutes) |
03 | CookVegetable(vegetable, Steam, timeInMinutes); |
06 | public void FryVegetable(Vegetable vegetable, int timeInMinutes) |
08 | CookVegetable(vegetable, Fry, timeInMinutes); |
11 | public void BakeVegetable(Vegetable vegetable, int timeInMinutes) |
13 | CookVegetable(vegetable, Bake, timeInMinutes); |
16 | public void CookVegetable(Vegetable vegetable, |
17 | Action<Vegetable, CookingTime> cookingAction, |
21 | cookingAction(vegetable, Minutes.Is(timeInMinutes)); |
We got rid of the duplicated code the same way we did when we did our first refactor, except this time we parameterized method calls instead of data.
If you understood this, you understand Action<>. Action<> is just a way of treating methods like they are data. Now you can extract all of the common logic into a method and pass in data that changes as well as actions that change.
Congratulations, you are doing the strategy pattern without having to create an abstract base class and a huge inheritance tree!
So when you see Action<>, just think “ah, that means I am passing a method as data.”
It really is as simple as that.
Action<Vegetable, CookingTime> translated to English is: “A method that takes a Vegetable and a CookingTime as parameters and returns void.”
What about Func<>?
If you understand Action<>, you understand Func<>.
Func<X, Y, Z> translated to English is: “A method that takes an X, and a Y as parameters and returns a Z”.”
The only difference between Action<> and Func<> is that Func<>’s last template parameter is the return type. Func<>s have non-void return values.
Bonus: Predicate<> is a Func<> that always returns a boolean.
.......................................................
An Action is a type of delegate:
- It returns no value.
- It may take 0 parameter to 16 parameters.
For example the following Action can encapsulate a method taking two integers as input parameters and returning void.

So if you have a method like below:

You can encapsulate the method Display in Action MyDelegate as below:

An Action with one input parameter is defined in the System namespace as below:

Where in T is a type of input parameter and a T object is a value passed for the parameter.
Action with Anonymous method
You can work with Action and anonymous methods as well. You can assign an anonymous method to an Action as below:

The above code will print 9 as output.
Action with Lambda Expression
Like any other delegates, an Action can be created with a lambda expression also, as below:

The above code will also print 9 as output.
Passing Action as input parameter
You can pass an Action as a parameter of a function also. Let us say you have a class:

And two functions called Display and Show to display Name and RollNumber of Student.

Now you have a function where you need to pass either Display or Show. Or in other words you need to pass any function with the same signature of Display or Show. In that case you will be passing a delegate as an input parameter to the function.

You can call the CallingAction method in Main as below:
