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?

10
  • 3
    you need to specify the lang version in your project either by editing the csproj or using the build settings Commented Dec 10, 2019 at 21:47
  • 4
    Project properties, build tab, set the language version. Commented Dec 10, 2019 at 21:48
  • 3
    are you on the latest version of Visual Studio? Commented Dec 10, 2019 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. Commented Dec 10, 2019 at 22:01
  • You can use C# 8 with VS2017 by installing the compilers nuget package. However, you won't have good intellisense support for C#8 features. Commented Dec 10, 2019 at 22:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.