How to enable C# 9.0-preview

后端 未结 3 456
太阳男子
太阳男子 2020-12-18 18:14

I have downloaded and installed v5.0.0-preview.5. My project is targeting net5.0 but C# 9.0 is not working. How can I enable C#

相关标签:
3条回答
  • 2020-12-18 19:02

    As of October 2020:

    Please see @Pac0's answer here: https://stackoverflow.com/a/64386529/159145

    As of June 2020:

    According to this page in the documentation you need to edit your *.csproj to set the <LangVersion> to preview.

    Also mentioned in the blog-post about the preview-release, but not the above documentation page, is that you need to update your project's targetFramework property too to net5.0 (this is because the C# design team decided to restrict entire C# language versions to minimum BCL versions, unlike previously where you could use C# 7 with even .NET Framework 2.0 provided you reimplemented your own missing BCL types like ValueTuple and ExtensionAttribute).

    So your *.csproj file should look like this:

    <Project>
     <PropertyGroup>
       <LangVersion>preview</LangVersion>
       <TargetFramework>net5.0</TargetFramework>
     </PropertyGroup>
    </Project>
    
    0 讨论(0)
  • 2020-12-18 19:10

    As per October 2020,

    1. you can explicitly use the 9.0 language version in .csproj
    2. Using target framework as .net 5 implicitly uses C# 9 by default .

    The .csproj should be as such:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
      </PropertyGroup>
    </Project>
    

    You can add <LangVersion>9.0</LangVersion> if you wish, but it should be optional.

    OutputType can be adapted, of course, and you'll need the .NET 5 SDK.

    See for instance this blog for more information.

    0 讨论(0)
  • 2020-12-18 19:12

    Firstly Download .NET 5 and then install Visual Studio Preview Edition. You will now have access to the latest features of C# 9. Also make sure that you project file includes the following.

     <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <LangVersion>preview</LangVersion>
    
    0 讨论(0)
提交回复
热议问题