switch文のオプションとしてenumを使用しています。問題は、ユーザーが無効なオプションを入力してプログラムがクラッシュした場合です。デフォルトを使用するように追加する必要はありますか?列挙型プログラムがクラッシュで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
。代わりにEnum.Parse
使用Enum.TryParse
の
switch (options)
{
case Options.Display: //dispaly regular time
case Options.Toggle://toggle
default:
Console.WriteLine("entry blah blah");
break;
あなたは、ランタイムエラーのどのような種類を得るのですか? – dasblinkenlight