2

我正在看这段代码:

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;
    }
}

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

无法推断隐式类型解构变量“msg”的类型,对于 isCC 和 upd 也是如此。

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

4

2 回答 2

5

case标签与 switch 表达式一起使用;,中间有 a,;后面没有:

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
};
于 2019-11-23T15:19:57.573 回答
0

我没有检查就写,但你可以尝试这样的事情:

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

然后像这样使用它:

result.msg
于 2019-11-23T15:12:12.843 回答