What is the assignment in Action parameter method for?

Asked

Viewed 276 times

6

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

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

My question is what is the assignment for - long id = 0, String tema = "" whether in that context it is really attribution in the codes. Virtually all Actions and various methods contain this form of declaration.

1 answer

5


Optional parameters

The assignment in the parameter exists so that when being called a Action Teste and no parameters are passed id and tema, to Action assign the default values 0 and "" respectively.

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

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

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

@Html.Action("Teste");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.