user242121
user242121

Reputation: 153

Action <T> usage as parameter

I just started with .net core and found Action<T> used everywhere. I have provide sample code from Swagger code block below. My question is what is the use of using Action<T> here? I need to pass config data. How does Swagger extract that configuration data?

 services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "My API",
                Description = "My First ASP.NET Core Web API",
                TermsOfService = "None",
                Contact = new Contact() { Name = "Talking Dotnet", Email = "[email protected]", Url = "www.x.com" }
            });
        });5

Upvotes: 15

Views: 16560

Answers (2)

John Wu
John Wu

Reputation: 52240

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).

Upvotes: 8

George Helyar
George Helyar

Reputation: 5278

It's a lambda function which does not return anything. You could supply a void returning method there.

Here it's just used so that you can supply a function that does something with T. It means the library can create a default options object and give you a way of modifying it.

The method would be doing something like

public void AddFoo(Action<FooOptions> configure) {
    // Create a default configuration object
    var options = new FooOptions();

    // Let the action modify it
    configure(options);

    // Do something with the now configured options
}

Upvotes: 22

Related Questions