1

我有这段代码,其中开关的每个部分都返回一个值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;
}
4

3 回答 3

2

您的代码类似于以下代码,但如果设置或其他属性有副作用,ModeMessage2那么事情发生的顺序可能很重要,在这种情况下,这在技术上并不是 100% 等效的。

IList<MO> specialModes = new[] { MO.Learn, MO.Practice, MO.Quiz };

if (specialModes.Contains(Settings.Mode) && Settings.Cc == CC.H) {
   Settings.Cc = CC.JLPT5;
   App.cardSetWithWordCount = null;
   App.DB.RemoveSelected();
}

ModeMessage2 = Settings.Mode switch {
   MO.Learn => "Use this mode when you are first learning the phrases and their meanings.",
   MO.Practice => "Use this mode to help you memorize the phrases and their meanings.",
   MO.Quiz => "Use this mode to run a self marked test.",
   _ => "Unknown mode value" // or throw
};

if (Settings.Mode == MO.Quiz)
   App.DB.UpdSet(SET.Adp, false);
于 2019-11-22T17:12:48.700 回答
1

你应该使用字典来做到这一点——

 Dictionary<TypeOf(Settings.Mode), string> map = new Dictionary<TypeOf(Settings.Mode), string>();

 map.Add(MO.Learn,"Use this mode when you are first learning the phrases and their meanings.");
 map.Add(MO.Practice,"Use this mode to help you memorize the phrases and their meanings.");
 map.Add(MO.Quiz,"Use this mode to run a self marked test.");


 ModeMessage2 = map[Settings.mode]);

这将比任何 switch 语句都快得多并且更易于维护。

如果有意义,您也可以使用数组。


以下评论者请注意:我做出以下假设,在某些情况下可能是错误的,但在一般情况下并非如此。1) 代码的编写方式是“分配”在代码的整个生命周期内只发生一次——在这种情况下,如果地图被多次使用,您将获得节省,以便在 N 次之后分配成本消失到 0。2)我们不知道键的类型,假设它是一个字符串的注释正在做出一个可能不正确的假设。即便如此,字符串的任何“快速”比较都使用哈希,这与字典用来提高速度的方法相同。3) 众所周知,编程中最慢的事情就是分支。字典(或数组映射)允许您没有任何分支,只是对内存位置的计算。

于 2019-11-22T17:13:41.423 回答
1
var modeMessage2 = Settings.Mode switch
{
    MO.Learn => "Use this mode when you are first learning the phrases and their meanings.",
    MO.Practice => "Use this mode to help you memorize the phrases and their meanings.",
    MO.Quiz => "Use this mode to run a self marked test."
}

if (Settings.Cc == CC.H)
{
    Settings.Cc = CC.JLPT5;
    App.cardSetWithWordCount = null;
    App.DB.RemoveSelected();
}

if (Settings.Mode == MO.Quiz) {
    App.DB.UpdSet(SET.Adp, false);
}

于 2019-11-22T17:07:40.190 回答