1

For one of our projects we're trying to upgrade to use C#8 instead of C#7.1. The upgrade looks like it works, because the compiler looks like it takes C#8 statements (it doesn't mark it as an error). However, when I build the project I get a build failed without any errors showing initially.

The code to test C#8 is of the following lines:

private string TestCSharp8(int x)
{
    return x switch
    {
        0 => "ZERO",
        1 => "ONE",
        2 => "TWO",
        3 => "THREE",
        _ => "OTHER"
    };
}

The IDE is accepting this code and even suggests to reformat it to this when writing an 'old-fashioned' switch statement. However, when I then build the solution I get the following output in the Error List: enter image description here

But then the Ouput log shows the following:

enter image description here

When I look at the build log after I put the Ouput verbosity to detailed, I can see the following errors occur:

1>C:\VisualStudio\Repos\XXX\YYY\ZZZ\Repositories\ABC.cs(301,5,301,6): error CS1597: Semicolon after method or accessor block is not valid
1>C:\VisualStudio\Repos\XXX\YYY\ZZZ\Repositories\ABC.cs(304,1,304,2): error CS1022: Type or namespace definition, or end-of-file expected
1>Done executing task "Csc" -- FAILED.
1>Done building target "CoreCompile" in project "XYZ.csproj" -- FAILED.

In the project file I have set the <LangVersion> to latest. The target framework we're targetting is .NET Framework 4.7.1.

The strange thing is all of this works and builds in another solution we're having.

So, can someone please help me in the right direction on how to fix this? At the moment I don't know where to look for the solution.

ZiNNED
  • 2,620
  • 20
  • 31
  • 1
    Have you seen this [link](https://stackoverflow.com/questions/56651472/does-c-sharp-8-support-the-net-framework) question? – Miamy Mar 24 '20 at 10:12
  • 1
    Type `#error version` in your program and look at the error message both from the IDE and the build log. This will confirm what compiler and language versions are used and will help troubleshoot. – Julien Couvreur Mar 24 '20 at 15:17

2 Answers2

5

From the docs:

C# 8.0 is supported on .NET Core 3.x and .NET Standard 2.1.

And:

C# 8.0 (and higher) is supported only on .NET Core 3.x and newer versions. Many of the newest features require library and runtime features introduced in .NET Core 3.x.

mm8
  • 163,881
  • 10
  • 57
  • 88
1

I found the answer which helped me solve this. In my case I had to update the Microsoft.Net.Compilers package to the latest version as well. Now it builds without any errors.

ZiNNED
  • 2,620
  • 20
  • 31
  • 3
    Until you try to use a feature that requires (.NET Core 3.x+) runtime support. – mm8 Mar 24 '20 at 10:58
  • True, but according to the link provided by @Miamy (https://stackoverflow.com/questions/56651472/does-c-sharp-8-support-the-net-framework), most features will work, which is good enough for us at the moment. – ZiNNED Mar 24 '20 at 11:54