I want the user to be able to enter a selection and that the program matches their selection using one of the parameters enum. I configured it in a loop, so the user can continue to try, if their record does not match (ie Apple, Banana, Carrots).
enum Food {Apple, Banana, Carrot};
Food foodChoice;
while (!(Enum.TryParse<Food>(Console.ReadLine(), true, out foodChoice)))
{
Console.WriteLine("Not a valid choice.");
}
All this works fine and dandy until the user logs in, say 5. Obviously, there are Foodnot so many options in the enumeration , but TryParseit will still be displayed true, assigning foodChoiceto 5. Is there an easy way to handle this?
source
share