Reputation: 19
I have a Web app that is targeting .Net 4.7 and had an if/else block to see if an incoming message contains a particular string. Ugly, I wanted to use a switch statement instead like so and like I did in another project, a windows service targeting .Net 4.5.2 that compiles and works:
switch (message)
{
case "SomeString":
...
break;
case var b when b.Contains("AnotherString"): // problem line
....
break;
}
But in the WebApp in question I get syntax errors upon build as if I'm not using C# 7.x as these, all pointing to the:
case var b when b.Contains("AnotherString"):
line:
Error CS1002 ; expected
Error CS1002 ; expected
Error CS1003 Syntax error, ':' expected
Error CS1513 } expected
The Crazy thing is that the Windows Service is targeting .Net 4.5.2 yet if accepts that line builds and and runs. Google says your C# version is predicated on what .Net version you are targeting.
What the actual frick?
Backed the Web App .Net 4.5.2 as is in the service project, but that did not change anything. Also changed the C# from "Latest major version" to 7.0 but got a different error that is a whole nuther group of google rabbit holes.
Update I am using VS2017 - when I add <LangVersion>7</Langversion>
to my .csproj or if I set the C# language version to 7 in the Visual Studio Build Settings GUI, I get the following error:
Error CS1617 Invalid option '7' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6.
This is the google rabbit hole error I spoke of but could not recall or fix.
Hey! There, I fixed it! My csproj file had yet references to DotNetCompliers 1.0.0. Right next to the reference for 4.2.0 (latest at the time of this writing) So I deleted that 1.0.0 refenrence. After that the debug session would not start because project/bin/roslyn/csc could not be found so I copied the folder over from another project. after doing that, I then got the complaint that "latest" for the langVersion had to be 1-6. So I set it to 7 - no go as before, crestfallen, I set it to 6 and then it worked! c# 7 features and all! it builds and runs and the switch case var b when b.Contains("string") works also In addition to that I can use other c#7 features too now.
That sucked. Now me so happy! Thanks everyone!
Upvotes: 0
Views: 123