[英]How do I declare parameters returned from a C# 8 switch expression?
I am looking at this code:我在看这段代码:
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;
}
}
Unfortunately it seems that the msg
, isCC
and upd
are not declared correctly and it gives a message saying this:不幸的是,似乎
msg
、 isCC
和upd
没有正确声明,它给出了一条消息:
Cannot infer the type of implicitly-typed deconstruction variable 'msg' and the same for isCC and upd.
无法推断隐式类型的解构变量 'msg' 的类型,对于 isCC 和 upd 也是如此。
Can you help explain to me how I can declare these?你能帮我解释一下我如何申报这些吗?
case
labels are not used with switch expressions, you have a ;
case
标签不与 switch 表达式一起使用,你有一个;
in the middle, and no ;
在中间,没有
;
after:后:
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
};
I am writing without checking, but you might try something like this:我写的时候没有检查,但你可以尝试这样的事情:
(string msg, bool isCC, bool upd) result = Settings.Mode switch ... <rest of your code>
Then use it like this:然后像这样使用它:
result.msg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.