That is, I have a method such as the following:
public static int CreateTaskGroup(string TaskGroupName,
string Market = "en-us", string Project = "MyProject",
string Team = "DefaultTeam", string SatelliteID="abc");
I would like to call this method from the command line, by reading the standard array of command line arguments. The obvious way to do it would be as follows:
if (args.Length == 1) CreateTaskGroup(args[0]);
if (args.Length == 2) CreateTaskGroup(args[0], args[1]);
if (args.Length == 3) CreateTaskGroup(args[0], args[1], args[2]);
Is it possible to do this in a more concise way?
Here’s one alternative, with the downside that you have to redeclare the default value constants:
You can reduce this issue by declaring the strings as
consts, e.g.:But then it’s not guaranteed by the compiler, nor overtly obvious, that
MarketDefaultis, in fact, still (code can be refactored in the future) the default forMarket.Edit: Here’s an alternate solution, using reflection:
This can be a bit hard to read, and won’t be too fast, but it does what you asked, without resorting to repetitive code, or having any uncertainty as to the default value of the parameters. You’ll probably want to add some error handling for too few or too many parameters. I prefer this solution.