I am unable to use some piece of code introduced in c# version 8.0.
Consider this piece of code.
Property patterns
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
};
Error:
Please help me solve this , infact I am using latest version of vs code.
Folks who have worked with functional languages will probably be familiar with the concept behind the recursive pattern matching C# 8 language feature. This technique can be used to validate and inspect objects more easily based on their shape.
To use Preview features, use the 'preview' language version. Show activity on this post. In Project → Properties → Build Select Advanced… (Advanced Build Settings) Show activity on this post. I my case I needed to check this checkbox (and restart Visual Studio!)
As we continue to iterate on these features, the list of preview features will fluctuate for each Visual Studio release as some features get added into the product and others are cut. You can learn more about the preview features available in each release in the release notes. Comments are closed.
Visual Studio 2019 Preview 2 is out! And with it, a couple more C# 8.0 features are ready for you to try. It’s mostly about pattern matching, though I’ll touch on a few other news and changes at the end. When C# 7.0 introduced pattern matching we said that we expected to add more patterns in more places in the future. That time has come!
You need to tell VS Code that you want to use a newer (preview) language version of c#. Are you working on a project? If so you can put <LangVersion>preview</LangVersion>
in the .csproj file. This might resolve the issue. You need to put it in the right place however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With