How to implement the following code without goto
and unnecessary code complexity?
namespace Test
{
static class Program
{
bool keyA = false;
bool keyB = false;
// ...
static void Main(string[] args)
{
foreach (string arg in args)
{
switch (arg.ToLowerInvariant())
{
case "-a":
if(keyA) break;
keyA = true;
// do here also what the A key requires
break;
case "-b":
if(keyB) break;
keyB = true;
// do here also what the B key requires
goto case "-a"; // key B includes the action of key A
// ...
}
}
}
}
}
The only thing that suggests itself is to duplicate the lines of code from key A to key B or use stuffing everything into methods, but this doesn’t look convenient and compact to view.
1