Reputation: 42957
I am pretty new to C# .NET and I have the following doubt.
On a page on which I am working on I found the following link:
<a class="ui-btn-inline ui-btn ui-icon-delete ui-btn-icon-notext ui-corner-all" href="@Url.Action("Delete", "Groups", new { id = item.gruppoId })">Delete</a>
This link call a Delete() method on a GroupsController class.
Ok, this is this method:
public ActionResult Delete(int id = 0)
{
.......................
.......................
.......................
DO SOME STUFF
.......................
.......................
.......................
return View(model);
}
My doubt is related to the signature of this method: why is the parameter int id = 0 ?
What does the = 0 mean? To begin with I thought that this was a simple initialization and that it change the it value to 0 but using the debbugger I discovered that it don't change the id value. So what it exactly do?
Upvotes: 0
Views: 93
Reputation: 13783
You're right in saying that the = 0
sets the value of the id
parameter.
But it's important to note that it only does so when you do not pass that parameter.
Take for example:
public void SaySomething( var something = "Hello" )
{
Console.WriteLine( something );
}
//...
SaySomething();
SaySomething("I am sleeping.");
The first call to the function does not pass a parameter. Therefore, the default value "Hello"
is used to write to the console.
The second call already sets a value for the parameter, so it does not get overwritten by the default value you set up. "I am sleeping."
will be printed in this case.
Upvotes: 3
Reputation: 415790
It's called an optional parameter. It means you can call the method with no argument, like this:
Delete();
If you do that, the value of the id
parameter in the function will be 0
.
Upvotes: 8