如何从 C# 8 switch 表达式 return 转换为 属性 和两个本地声明的变量?
How can I return from a C# 8 switch expression into a property and two locally declared variables?
我有这段代码,效果很好:
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),
_ => ("", false, false)
};
但在实际的应用程序中,我想将一个值 return 转换为 属性 并在此处分配给多个位置:
string _modeMessage2;
public string ModeMessage2 { get => _modeMessage2; set => SetProperty(ref _modeMessage2, value); }
尝试这行不通:
var (this.ModeMessage2, 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),
_ => ("", false, false)
};
有谁知道如何在不添加如下代码的情况下 return 将值 return 放入 ModeMesage2 中?
ModeMessage2 = msg;
如果左边的所有项目都是字段或属性,那么它将起作用,如果您随后尝试将其中一项更改为 var isCC
之类的表达式,那么您将收到一个编译器错误,提示您不能混合表达式和声明。
很遗憾,这不受支持。
我有这段代码,效果很好:
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),
_ => ("", false, false)
};
但在实际的应用程序中,我想将一个值 return 转换为 属性 并在此处分配给多个位置:
string _modeMessage2;
public string ModeMessage2 { get => _modeMessage2; set => SetProperty(ref _modeMessage2, value); }
尝试这行不通:
var (this.ModeMessage2, 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),
_ => ("", false, false)
};
有谁知道如何在不添加如下代码的情况下 return 将值 return 放入 ModeMesage2 中?
ModeMessage2 = msg;
如果左边的所有项目都是字段或属性,那么它将起作用,如果您随后尝试将其中一项更改为 var isCC
之类的表达式,那么您将收到一个编译器错误,提示您不能混合表达式和声明。
很遗憾,这不受支持。