有没有办法使用 C# 8 switch 表达式切换到 return 字符串值?

Is there a way for switch to return a string value using C# 8 switch expressions?

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

您的代码类似于以下代码,但如果设置 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);

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

 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) 代码的编写方式是 "allocation" 在代码的生命周期内只发生一次——在这种情况下,如果多次使用地图,您将节省开支,以便在 N 次之后的成本分配变为 0。 2) 我们不知道密钥的类型,假设它是一个字符串的注释正在做一个可能不正确的假设。即便如此,任何 "fast" 字符串比较都使用哈希,这与字典用来提高速度的哈希相同。 3) 众所周知,在编程中你能做的最慢的事情就是分支。字典(或数组映射)允许您没有任何分支,只是对内存位置的计算。