I am writing a c# console app that will take a user input from a list of selected options and do things based on the selected option. I have it working right now but just looking over it there seems that I should be able to shorten the code with other methods and make it perform a bit faster and more efficient but for some reason I just can't see it. Am I missing something or is this really the way to be doing this check?
static void Main(string[] arguments)
{
if (arguments.Length == 0)
{
Console.WriteLine("No run mode entered. Usage: JobSightMaintenance <FirstRun | NightlyMaintenance>");
Console.WriteLine("Please select one of the following options to run:");
printOptions();
string userInput;
bool isValidInput = true;
do
{
userInput = Console.ReadLine();
int selectedOption;
try
{
selectedOption = int.Parse(userInput);
}
catch (Exception ex)
{
Console.WriteLine("Invalid Option, please try again");
printOptions();
isValidInput = false;
break;
}
switch (selectedOption)
{
case 1:
isValidInput = true;
break;
case 2:
isValidInput = true;
break;
default:
Console.WriteLine("Invalid Option, please try again");
printOptions();
isValidInput = false;
break;
}
}
while (!isValidInput);
}
if (Debugger.IsAttached) { Console.ReadKey(); }
}
private static void printOptions()
{
Console.WriteLine("1. Run Intial Data Import");
Console.WriteLine("2. Nightly Maintenance");
}
int.TryParse()
instead oftry/catch
block \$\endgroup\$