This is an alternative pattern I sometimes use for handling a mix of optional and default parameters:
private string[] defaultData = new []{"Id", "DataModified"};
public bool myMethod(bool ensureDefaultDataIsUsed = true, params string[] data)
{
if (ensureDefaultDataIsUsed)
{
data = data.Concat(defaultData).Distinct().ToArray();
}
return true;
}
Consider these usage examples:
bool m0 = myMethod();
bool m1 = myMethod(data: "hello");
bool m2 = myMethod(true, "hello", "goodbye");
bool m3 = myMethod(false, "hello", "goodbye");
bool m4 = myMethod("hello");
What I like about this:
1 keeping default parameters in a variable makes it easy to change what the defaults are
2 all the options are in one method
3 control over whether defaults are used, ignored, or combined with user supplied data
What some might consider the downside: example #m4 illustrates that using an explicitly named parameter for 'ensureDefaultDataIsUsed is required to avoid a clash between a positional and an optional value.