我使用enum作爲switch語句的選項,並且它可以工作。問題是如果用戶輸入一個無效的選項程序崩潰。我應該添加什麼,以便使用默認值?使用enum和switch語句c#
我枚舉類
public enum Options : byte
{
Display = 1,
Add,
Toggle,
Max,
Mean,
Medium,
Exit
}
在枚舉程序崩潰主我switch語句
string volString = Console.ReadLine();
Options options = (Options)Enum.Parse(typeof(Options), volString);
// this is the line that is giving me the runtime error. Since other options are not found
。
switch (options)
{
case Options.Display: //dispaly regular time
case Options.Toggle://toggle
default:
Console.WriteLine("entry blah blah");
break;
你會得到什麼樣的運行時錯誤的? – dasblinkenlight 2012-01-08 00:12:01