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

Ala*_*an2 2 c# c#-8.0 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是msgisCC和似乎upd没有正确声明,它给出了一条消息,说明:

无法推断出隐式类型的解构变量'msg'的类型,对于isCC和upd而言,类型相同。

您能帮我解释一下如何声明这些吗?

GSe*_*erg 5

case标签与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
};
Run Code Online (Sandbox Code Playgroud)