问题
I am not sure how to use new C# 7 features in existing solution. I tried using pattern matching in a switch statement but I keep getting Value of integral type expected
error.
Is there a trick to enable it? I though I can just use new features if I open the solution in VS 2017.
My projects are targeting .net 4.6.2.
Here is the sample code
private void CS7Test(object o)
{
switch (o)
{
case null:
Console.WriteLine("it's a constant pattern");
break;
case int i:
Console.WriteLine("it's an int");
break;
case UserInfo p when p.Username.StartsWith("Ka"):
Console.WriteLine($"a Ka person {p.Username}");
break;
case UserInfo p:
Console.WriteLine($"any other person {p.Username}");
break;
case var x:
Console.WriteLine($"it's a var pattern with the type {x?.GetType().Name} ");
break;
default:
break;
}
}
回答1:
Actually it's working without doing any special config. Resharper was giving those errors and after disabling the resharper it worked like a charm.
回答2:
Vs 2017 is configured by default to support c#7.0 with enabling Resharper. You need not to disable it.
In vs 2017 update 3 you can configure it to use the new features of c# 7.1 (and resharper is enabled also).
For more details review: setting c#7.1 in vs2017.3
来源:https://stackoverflow.com/questions/43021529/how-to-enable-c-sharp-7-features-in-existing-projects