如何声明从 C# 8 开关表达式返回的参数?

How do I declare parameters returned from a C# 8 switch expression?

我正在查看这段代码:

public enum MO
{
    Learn, Practice, Quiz
}

public enum CC
{ 
    H
}

public class SomeSettings
{
    public MO Mode { get; set; }
    public CC Cc { get; set; }
}

static void Main(string[] args)
{
    var Settings = new SomeSettings() { Cc = CC.H, Mode = MO.Practice };

    var (msg,isCC,upd) = Settings.Mode switch {
        case MO.Learn => ("Use this mode when you are first learning the phrases and their meanings.",
                          Settings.Cc == CC.H,
                          false),
        case MO.Practice => ("Use this mode to help you memorize the phrases and their meanings.",
                          Settings.Cc == CC.H,
                          false),
        case MO.Quiz => ("Use this mode to run a self marked test.",
                          Settings.Cc == CC.H,
                          true);
        _ => default;
    }
}

不幸的是,msgisCCupd 似乎没有正确声明,它给出了这样一条消息:

Cannot infer the type of implicitly-typed deconstruction variable 'msg' and the same for isCC and upd.

你能帮我解释一下我如何申报这些吗?

我写的时候没有检查,但你可以试试这样:

(string msg, bool isCC, bool upd) result = Settings.Mode switch ... <rest of your code>

然后像这样使用它:

result.msg

case 标签是 not used 和 switch 表达式,中间有一个 ;,后面没有 ;

var (msg, isCC, upd) = Settings.Mode switch {
    MO.Learn => ("Use this mode when you are first learning the phrases and their meanings.",
                        Settings.Cc == CC.H,
                        false),
    MO.Practice => ("Use this mode to help you memorize the phrases and their meanings.",
                        Settings.Cc == CC.H,
                        false),
    MO.Quiz => ("Use this mode to run a self marked test.",
                        Settings.Cc == CC.H,
                        true),
    _ => default
};