Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a C# 8.0 Console application?

Tags:

c#

c#-8.0

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?

like image 931
Wyck Avatar asked Dec 10 '19 21:12

Wyck


1 Answers

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.

like image 70
kofifus Avatar answered Oct 16 '22 09:10

kofifus