【发布时间】:2020-03-18 18:39:18
【问题描述】:
我有这段代码,其中开关的每个部分都向ModeMessage2 返回一个值。是否可以使用新的 C# switch 表达式(或任何其他代码优化)来优化 switch 的工作方式?
switch (Settings.Mode)
{
case MO.Learn:
ModeMessage2 =
"Use this mode when you are first learning the phrases and their meanings.";
if (Settings.Cc == CC.H)
{
Settings.Cc = CC.JLPT5;
App.cardSetWithWordCount = null;
App.DB.RemoveSelected();
}
break;
case MO.Practice:
ModeMessage2 =
"Use this mode to help you memorize the phrases and their meanings.";
if (Settings.Cc == CC.H)
{
Settings.Cc = CC.JLPT5;
App.cardSetWithWordCount = null;
App.DB.RemoveSelected();
}
break;
case MO.Quiz:
if (Settings.Cc == CC.H)
{
Settings.Cc = CC.JLPT5;
App.cardSetWithWordCount = null;
App.DB.RemoveSelected();
}
App.DB.UpdSet(SET.Adp, false);
ModeMessage2 =
"Use this mode to run a self marked test.";
break;
}
【问题讨论】:
-
开关没有返回值。您返回一个字符串,您需要在每个 case 语句中设置一个字符串变量。字符串变量将在“switch”语句之前定义。
标签: c# switch-statement c#-8.0 switch-expression