C# 模式匹配数组

标签 c# pattern-matching switch-expression

var x = new int[] { 1, 2 };
var y = x switch {
  { 1, 2 } => "yea",
  _ => "nay"
};

编译失败。

如何对数组进行模式匹配?

最佳答案

你必须像这样自己扩展数组的元素

var x = new int[] { 1, 2 };
var y = (x[0], x[1]) switch {
  (1, 2) => "yea",
  _ => "nay"
};

关于C# 模式匹配数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70901344/

相关文章:

c# - Windows Phone 构建应用程序教程

java - 为什么 BinaryReader 卡在 ReadString() 上而不卡在 Read() 上?

c# - 获取一个类的所有成员的集合

php - 寻找一种干净、高效的方法来将一组数据与已知模式进行匹配

时间:2019-05-07 标签:c#8switch表达式: No best type was found for the switch expression

c# - 将 Ping 应用程序转换为多线程版本以提高速度 - C#

bash - 如何匹配特定列中的多个模式?

r - 长转宽格式: keep row orders and use only part of row values for newly created column names

c# - 如果我的 C# switch 表达式不是详尽的,会发生什么?

c# - 有没有一种方法可以让 switch 使用 C# 8 switch 表达式返回字符串值?