When you see a variable or a parameter of type Action
, that means it is a reference to a method call. For example:
//Declare a method with no parameters
void ShowMessage()
{
Console.WriteLine("Hello world");
}
//Store a reference to that method in x
Action x = ShowMessage;
//Call that method
x(); //Displays "hello world"
Using a lambda expression, you can also define the method body inline, like this:
//Declare a lambda expression and store a reference to it in x
Action x = () => Console.WriteLine("Hello world");
//Call that method
x(); //Displays "hello world"
Now what if you need to store a reference to a method that takes parameters? Well, Action<T>
is generic, meaning that various kinds of Action<T>
can take parameters of different types. For example, an Action<string>
can accept a string parameter.
void ShowMessage(string message)
{
Console.WriteLine(message);
}
Action<string> x = ShowMessage;
x("Hello world"); //Displays "Hello world"
Or as a Lambda:
Action<string> x = message => Console.WriteLine(message);
x("Hello world"); //Displays "Hello world"
When a method accepts an action as an argument, is is typically going to be used as a callback. For example, LINQ's Where
method accepts a delegate that is executed for each item in a list and uses its output to determine whether the item should be included in the results.
With AddSwaggerGen
you are providing a reference to a method that Swashbuckle will call at some point. I believe the method in this case is supposed to generate Swagger (typically using SwaggerDoc
).