What is the assignment in the Action parameter, method?

6

I had a question about some of the codes that I found in a project of a former employee of the company. It is a code that contains assignment in the variables parameters. Here is an example:

public ActionResult Teste(long id = 0, String tema = "")
{
    //Lógica
}

My question is what is the assignment - long id = 0, String tema = "" if in this context is actually assignment in the codes. Virtually all Actions and several methods contain this form of declaration.

    
asked by anonymous 22.06.2015 / 15:26

1 answer

5

Optional Parameters

The assignment in the parameter exists so that if the Action Teste is called and the id and tema parameters are not passed, the Action is assigned the default values 0 and "" respectively.

In the example below the value the value of the id will be 1 and the theme "blue":

@Html.Action("Teste", new { id = 1, tema = "azul" });

But in this next example it will assume the values default : id will be 0 and the theme "".

@Html.Action("Teste");
    
22.06.2015 / 16:03