'How do I create a C# 8.0 Console application?
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.
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?
Solution 1:[1]
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
You should remove the
<LangVersion>latest</LangVersion>
from your project file when you update the .NET SDK.
Solution 2:[2]
I had the same problem (c#8 unavailable on Core 3.1 project) and I fixed it that way :
- Uninstall previous version of Visual studio (2015 and 2017 in my case)
- Repair Visual studio 2019 (V16.4.1 in my case) with "visual studio installer" launched with administrator right.
No need of LangVersion in csproj.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Michael Freidgeim |
Solution 2 | Anor |