Technical limitations
Exposing methods with optional parameters like APIs for other languages that do not support this functionality causes problems, in this case overloading methods is recommended.
Reflection operations also display incompatibility with optional parameters.
Functionality
Does your method do the same thing regardless of parameters?
If yes, use optional parameters if you do not use methods overload. Different features should be in different methods.
Taking the technical limitations and functionality, the next points are subjective , that is, preference, liking and alignment with your team / team of employee code:
Optional Parameters = Less Code
Method Overload:
public void Method(string str1)
{
// ...
}
public void Method(string str1, string str2)
{
// ...
}
Optional parameters:
public void Method(string str1, string str2 = null)
{
// ...
}
In the example above, only one method is required, significantly reducing the amount of code. Consequently, there will be less documentation with% of%.
Optional parameters = More concise Intellisense
With optional parameters, VS Intellisense displays the method on one line only with the optional parameters:
Withmethodoverload,youhavetonavigatethrougheachmethodtofindwhatyouwant:
Reference