Feature Recursive Patterns 目前在 Vs Code 的预览版中

Feature Recursive Patterns Is currently in Preview Version of Vs Code

我无法使用 c# version 8.0 中介绍的一些代码。

考虑这段代码。

属性 模式

class Address
{

    public string State { get; set; }
}

public static decimal ComputeSalesTax(Address location, decimal salePrice) =>
location switch
{
    { State: "WA" } => salePrice * 0.06M,
    { State: "MN" } => salePrice * 0.75M,
    { State: "MI" } => salePrice * 0.05M,
    // other cases removed for brevity...
    _ => 0M
};

错误:

请帮我解决这个问题,事实上我使用的是最新版本的 vs 代码。

您需要告诉 VS Code 您想要使用更新的(预览版)语言版本的 c#。你在做一个项目吗?如果是这样,您可以将 <LangVersion>preview</LangVersion> 放入 .csproj 文件中。这可能会解决问题。但是你需要把它放在正确的地方。