What to do ?
When trying to decide whether to use optional parameters, or define multiple methods with the same name, but different parameter lists (method overloading): consider optional parameters in one method definition when it
doesn't matter if that optional parameter has a value defined ... in other words, the code that executes is the same. However, if you have methods that use different Types of parameters, use method overloading. See: [
^].
What
not to do ?
A potential problem with optional parameters is that certain Types used as parameters will be auto-initialized to non-null values, and you may have trouble distinguishing whether the
caller passed you a valid value, or not.
For example:
private void MyMethod(int int1, string string1 = "", int int2 = 0)
In the body of this method, how will you know if the values of 'string1, and 'int1 have been
explicitly set by the caller, rather than having been set by default ? That can lead to quirky code work-arounds like this:
private void MyMethod(int int1, string string1 = null, int? int2 = null)
{
Console.WriteLine("{0}, {1}, {2}", int1, string1, int2);
if(string1 != null)
if(int2 != null)
}
Where you take advantage of the fact that Type 'String can be 'null, and you use a nullable-Int parameter to allow the integer entry to be null. I consider this a "code-smell."
Another strategy, if all your parameters are
the same Type, is to use the 'params keyword to allow a variable length Array of parameters:
private void MyMethodWithVariableParams(params int[] intParams)
{
foreach(int theInt in intParams)
{
}
}
This is most useful when you really want to
iterate over the parameter Array. It is less useful, when you have to write if/else statements, or a switch statement, to handle different lengths of the parameter Array.
In my opinion, I think it best to minimize use of optional parameters, but, if you do, you can take advantage of how the
calling syntax that uses Named Parameters allows you to skip parameters you don't want to set in a given method call.
I find it "unfortunate" that many people refer to "Named Parameters" as if they were another
type of parameter that can be used in declaring a Method, when, in fact, this is only an optional
syntax for calling a Method.
An interesting essay on possible drawbacks of using optional arguments: [
^].
I prefer to see method-overloading when there are distinct parameter-configurations that will be called frequently ... as long as: code common to all method-overloads is factored out into a separate method.
If you are using .NET 4.0 or later, and you really want to make your code smell good, you can use the 'OptionalParameter Attribute:
using System.Runtime.InteropServices;
private void MyMethod(int int1, [OptionalParameter] string1, [OptionalParameter] int2)
{
}
To use this attribute, you must reference the System.Runtime.InteropServices library: however, the variables will be initialized in the same way that optional variables defined without the Attribute are. Using this Attribute has no run-time performance penalty.