liveBook · Manning

Action

As explained in .NET in Action, Second Edition

Action is a .NET generic delegate family representing methods that accept parameters and return no value.

Action is a .NET generic delegate family for methods that accept parameters and return no value. Its type arguments describe the method parameters, such as Action<T> for one parameter. section 2.8

A method group, lambda expression, or anonymous method can satisfy an Action type when its parameter list matches. Action is distinct from ActionResult, which represents an ASP.NET response selected by a request-handling action. section 2.8 section 3.1.2

Common forms include:

  • Action<IEnumerable<Error>> accepts an error sequence and performs an operation without returning a value. section 2.8
  • Action<int, int> accepts two integers, while Action<string, string> accepts two strings. section 2.8
  • Use Func<TResult> forms when the delegate must return a value instead. section 2.8

Passing a callback to parsed command-line options

Parser.Default.ParseArguments<Options>(args).WithParsed<Options>(AsciiArt.Write) passes AsciiArt.Write as an Action<Options>. The callback receives the parsed Options object when parsing succeeds. section 2.8

Listing 2.18 Four different ways to write the same Action
void WriteUsageAndIgnoreErrors1(IEnumerable<Error> errors)   #1
{
  WriteLine("Usage: HelloDotnet <text> --font Big");
}
  
void WriteUsageAndIgnoreErrors2(IEnumerable<Error> _)        #2
{
  WriteLine("Usage: HelloDotnet <text> --font Big");
}
  
void CallToAction(IEnumerable<Error> errors)
{
  Action<IEnumerable<Error>> action1 = WriteUsageAndIgnoreErrors1;
  action1(errors);
  Action<IEnumerable<Error>> action2 = WriteUsageAndIgnoreErrors2;
  action2(errors);
  Action<IEnumerable<Error>> action3 =
    (IEnumerable<Error> _) =>                                #3
    {
      WriteLine("Usage: HelloDotnet <text> --font Big");
    }
  action3(errors);
  Action<IEnumerable<Error>> action4 = _ =>                  #4
    WriteLine("Usage: HelloDotnet <text> --font Big");
  action4(errors);
}

Compare a named method, ignored parameter, block-bodied lambda, and expression-bodied lambda that all satisfy the same Action<IEnumerable<Error>> contract. section 2.8

How .NET in Action, Second Edition covers it

How deep it goes
Defines parameter matching and demonstrates method groups, lambdas, and Func contrast, without descending into delegate runtime internals.
Examples
Uses AsciiArt.Write, error-handling callbacks, and Action<int, int> and Action<string, string> lambda examples.
What it assumes
Assumes familiarity with C# methods and parameters while teaching generic delegate signatures through focused examples.
What you can do after
Apply it via C# method groups or lambdas when an API expects a parameterized callback with no return value.
How it connects
Connects Action with delegates, method groups, lambda expressions, generic types, Func, and command-line parsing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.