6

I tried creating a Windows Forms .NET core 3.1 application via the template and switching the output type to Console Application.

Here's my code:

static class Program
{
    static void Main()
    {
        System.Console.WriteLine(0 switch { 0 => "Hello World" });
    }
}

When I compile I get:

error CS8370: Feature 'recursive patterns' is not available in C# 7.3. Please use language version 8.0 or greater.

I'm targeting .NET Core 3.1. I thought that would get me C# 8.0 language features by default. Apparently I am mistaken.

What do I do?

EDIT: I'm using Visual Studio 2019 16.3.9

This is the part that confuses me the most because it says that the Language version is "Automatically selected based on framework version" (and I can't change it.) Also I don't see an adequate explanation of why I can't change language versions at Why can't I select a different C# version? That page says that if I'm using .NET Core 3.x that I should be using C# 8.0.

enter image description here

The .csproj file is as follows:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <ApplicationIcon />
    <StartupObject>Program</StartupObject>
  </PropertyGroup>

</Project>

Adding this line fixes the problem:

    <LangVersion>8.0</LangVersion>

But is that really the only way to create an application? I have to manually edit my .csproj? Why can I not change the Language version and why is it not automatically selecting C# 8.0 based on me using .NET Core 3.1?

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • 3
    you need to specify the lang version in your project either by editing the csproj or using the [build settings](https://codeopinion.com/specify-c-version-use-projects/) – JSteward Dec 10 '19 at 21:47
  • 4
    Project properties, build tab, set the language version. –  Dec 10 '19 at 21:48
  • 3
    are you on the latest version of Visual Studio? – MikeT Dec 10 '19 at 21:53
  • In the place Amy describes, I think you can only _change_ the C# version if you are on Visual Studio 2017 or older, but VS2017 is too old for C# 8 (Michael Tranchida's point). Edit: However, if you had _only_ VS2017, the error text would probably be more obscure, instead of this description of the difference between C# 7.3 and 8.0. – Jeppe Stig Nielsen Dec 10 '19 at 22:01
  • [You can use C# 8 with VS2017](https://stackoverflow.com/questions/54701377/how-can-i-use-c-sharp-8-with-visual-studio-2017) by installing the compilers nuget package. However, you won't have good intellisense support for C#8 features. –  Dec 10 '19 at 22:08
  • I like to use Visual Studio Preview to try these new things. https://visualstudio.microsoft.com/vs/pre It can be installed side by side with other Visual Studio version. – Tony Dec 10 '19 at 23:12
  • Please show csproj file. – Lukasz Szczygielek Dec 10 '19 at 23:14
  • I am using Visual Studio 2019. – Wyck Dec 10 '19 at 23:17
  • Saying "I am using Visual Studio 2019" is equivalent to nothing. VS2019 has been updated so frequent that without a specific version number (such as 16.4) no one knows what you are using. With a recent enough VS version, edit your project file https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version – Lex Li Dec 10 '19 at 23:22
  • No repro. I created a new .NET Core 3.1 Winforms application, added this code and got no error. Changing to `Console` also didn't cause any problems. Post your csproj contents. I didn't have to specify the language. – Panagiotis Kanavos Dec 11 '19 at 09:03

2 Answers2

8

Open your csproj and see if you have a line like

    <LangVersion>7.3</LangVersion>

If yes try removing it, if that doesn't work try to change it to 8.0

From https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#defaults

You should remove the <LangVersion>latest</LangVersion> from your project file when you update the .NET SDK.

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
kofifus
  • 17,260
  • 17
  • 99
  • 173
  • 3
    I did **not** have that line, or any line that specifies `LangVersion`. But if I add one, and change it to 8.0, then it compiles. So thanks for unblocking me! But this doesn't really explain the full experience I was having as to why I couldn't change versions and why the correct version wasn't being detected automatically. – Wyck Dec 11 '19 at 14:14
0

I had the same problem (c#8 unavailable on Core 3.1 project) and I fixed it that way :

  1. Uninstall previous version of Visual studio (2015 and 2017 in my case)
  2. Repair Visual studio 2019 (V16.4.1 in my case) with "visual studio installer" launched with administrator right.

No need of LangVersion in csproj.

Anor
  • 46
  • 2