Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 779
user242121 Avatar asked Jul 06 '18 20:07

user242121


People also ask

What is action t used for in an application?

You can use the Action<T> delegate to pass a method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate.

What is an action parameter?

Use parameter actions to let your audience change a parameter value through direct interaction with a viz, such as clicking or selecting a mark. You can use parameter actions with reference lines, calculations, filters, and SQL queries, and to customize how you display data in your visualizations.

What can you use to pass parameters to actions?

You can pass them via a URL, a query string, a request header, a request body, or even a form.

How do you pass a function as a parameter in C#?

If we want to pass a function that does not return a value, we have to use the Action<> delegate in C#. The Action<T> delegate works just like the function delegate; it is used to define a function with the T parameter. We can use the Action<> delegate to pass a function as a parameter to another function.


2 Answers

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
}
like image 64
George Helyar Avatar answered Sep 29 '22 19:09

George Helyar


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

like image 32
John Wu Avatar answered Sep 29 '22 20:09

John Wu