gpt4 book ai didi

c# - 如何声明从 C# 8 switch 表达式返回的参数?

转载 作者:行者123 更新时间:2023-12-01 20:16:24 27 4
gpt4 key购买 nike

我正在查看这段代码:

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.

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

最佳答案

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

关于c# - 如何声明从 C# 8 switch 表达式返回的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59008718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com