Simple Action and Action<string> Examples

Full source code available here.

A junior engineer colleague of mine recently asked me “how the f* * * do I call this method?”. Seemed like a reasonable question. It was a method that took a complicated Func and an Action with a series of parameters. We broke it down into each part and I explained them to him separately.

I think this is a problem that more people have so I going to try to give some simple examples that hopefully you can learn from and build on yourself.

This post will talk only about Actions, a later post will talk about Funcs. I’m not going to go into the details of delegates, anonymous methods, how they get called, when they get executed, etc, there are plenty of blog posts and articles out there on those topics already. Instead, I’m going to focus on a few examples and explain them.

Example 1

This method takes an Action as a parameter, the Action itself has no parameters.

1public static void Printer(Action myPrinterAction)
2{
3    myPrinterAction();
4}

How do we call this? Here are a few examples. Create a second method -

1public static void PrinterMethod()
2{
3    Console.WriteLine("Hello from PrinterMethod");
4}

Now you can call Printer(..) like this –

Printer(PrinterMethod);

Or this

Printer(() => PrinterMethod());

To shortcut this, you can use a lambda as the parameter to Printer(..). The lambda must take no arguments and return nothing because that is how the Action, the Printer method task is defined.

Printer(() => Console.WriteLine("Hello from Console.WriteLine"));

Generally, you will want to go for the lambda approach as many people consider it the easiest to read.

There are more ways of calling Printer(..). You can explicitly define an Action and assign the PrinterMethod to it.

1Action printerActionAsMethod = PrinterMethod;
2Printer(printerActionAsMethod);

You can explicitly define an Action and assign a lambda to it.

1Action printerActionAsLambda = () => Console.WriteLine("Explicitly defined printer action");
2Printer(printerActionAsLambda);

There are other ways, but I think these are going to cover most of your needs.

Example 2

In this example the Printer method takes an Action as a parameter, this Action method takes a string as a parameter. The Printer(..) method also takes a string, this string is what the Action method will print.

1public static void Printer(Action<string> myPrinterAction, string value)
2{
3    myPrinterAction(value);
4}

How we call this is a little different than above. Here are a few examples. Create a second method -

1public static void PrinterMethod(string text)
2{
3    Console.WriteLine($"Hello from PrinterMethod - {text}");
4}

Then use it like this –

Printer(PrinterMethod, "Hi there");

Or this -

Printer((text) => PrinterMethod(text), "Calling print method");

Again, a lambda is probably the better approach. The lambda takes a single string as the argument and returns nothing because that is how the Action the Printer(..) method task is defined.

Printer(someMessage => Console.WriteLine($"Hello from Console.WriteLine - {someMessage}"), "this is a lambda");

The included source code shows a few other ways of making the calls, but I think the ones discussed here will cover most of the scenarios you will hit.

Full source code available here.

comments powered by Disqus

Related