The concept is called "default values" and allows for a different concept, "optional parameters".
Simply put, if you call the method and doesn't supply the specified parameter, its default value will be used.
But what does it mean?
There's two ways this concept could be interpreted by the compiler:
- Calls to this method that doesn't supply this parameter will end up using the default value compiled into the method being called.
- Calls to this method that doesn't supply this parameter will pass in the default value that was declared, at the time of compiling the code that calls it.
So which is it?
Let's look at two separate pieces of code:
public void Test(int value = 42)
{
}
and
public void SomeOtherTest()
{
SomeInstance.Test(); // this is the same Test as above
}
Let's assume these two are in different projects, and will thus be compiled into different assemblies, dll's.
What happens at compile time for that second piece of code is that the compiler sees that the default value for the missing parameter, value
, is 42
, and thus adds this to the compiled code.
In other words, the second piece of code is treated as though it was written like this:
public void SomeOtherTest()
{
SomeInstance.Test(42); // this is the same Test as above
}
Note the additional 42
in the parameter call here.
So the syntax in the question means two things:
- A parameter with a default value can be ommitted in calls to the method
- Such calls, where the parameter is ommitted, will be compiled as though the parameter value was there all along, with the default value for that parameter.
Why is this important?
Well, consider if the two pieces of code above was present in two different projects, and thus compiled into two different assemblies, dll's.
If you later change the source code of the project that declares that method, to provide a different default value, the other project is still compiled to supply 42.
Unless the other assembly, where the call is being made, is recompiled, the value 42
will still be passed to the method.