【发布时间】:2021-09-22 13:01:08
【问题描述】:
当使用带有 or 的 switch 表达式和带有条件的默认守卫时,预期的行为是什么? 我希望任何符合 or 语句之一或默认守卫条件的东西都会进入这种情况,但显然这不是它的工作原理。
考虑以下示例
var types = new HashSet<string>();
types.Add("a");
types.Add("b");
types.Add("c");
var input = "d";
var result = input switch {
"d" or _ when types.Contains(input) => "1",
_ => "2",
};
Console.WriteLine(result); //I'd expect "1" but I get "2"
【问题讨论】:
标签: c# .net switch-statement