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# 9.0?
- 187,200
 - 47
 - 362
 - 445
 
- 341
 - 1
 - 3
 - 6
 
- 
                    Are you using Visual Studio 2019, Visual Studio Code, or just using the command-line and/or MSBuild? – Dai Jun 15 '20 at 23:47
 - 
                    Visual Studio 16.6.2. My project target net5.0 and build is successful but when I use any C# 9.0 feature it does not work and build fails – Jerzy Grzelec Jun 16 '20 at 00:01
 - 
                    Please post the exact compiler output error message from the Output window (not the Error List window), including the line that shows `csc` being invoked by MSBuild. – Dai Jun 16 '20 at 00:02
 - 
                    And as I asked earlier, please post the results of what happens when you try to build your project by calling `csc.exe` directly from the command-line without using VS or MSBuild. – Dai Jun 16 '20 at 00:02
 - 
                    2I think that the version for c# 9 in dotnet 5 preview 5 is the same as in dotnet 5 preview 4. Using the instructions below one can compile the sample code from the preview 4 release notes, but you don't get the exciting "hot" bits from Mads presentation from May 20. eg. data class doesn't work nor does the "init" keyword for read-only properties. – Mike Wodarczyk Jun 16 '20 at 03:27
 - 
                    2You can track features, which are implemented already in [feature status](https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md) page. Records are still in progress – Pavel Anikhouski Jun 16 '20 at 07:27
 - 
                    @JerzyGrzelec the VS version you use is too old. Even with the latest Preview for both VS and .NET Core though, only a couple of C# 9 features are available. A better question would be `How can I use C# 9's feature X?` – Panagiotis Kanavos Jun 25 '20 at 06:59
 - 
                    @PavelAnikhouski how can Linqpad use records? Even sharplab.io chokes. Is an extra library or switch needed? – Panagiotis Kanavos Jun 25 '20 at 07:00
 - 
                    @PanagiotisKanavos In sharplab you can switch to roslyn branch https://imgur.com/a/xWcCHJd – Pavel Anikhouski Jun 25 '20 at 07:06
 - 
                    @PavelAnikhouski chokes unless you create an empty `IsExternalInit` property. .NET 5 Preview 5 though doesn't even recognise `record`. So how does Linqpad 6 do it? – Panagiotis Kanavos Jun 25 '20 at 07:07
 - 
                    @PavelAnikhouski Preview 6 was released a few hours after the last comment. Records work with a little workaround now – Panagiotis Kanavos Jun 26 '20 at 12:09
 
4 Answers
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>
- 141,631
 - 28
 - 261
 - 374
 
- 
                    1For those who don't know how to open the project file `*.csproj`: Right click your project on the `Solution Explorer` and choose `Edit Project File`. – Second Person Shooter Jun 15 '20 at 23:36
 - 
                    @JerzyGrzelec Try building your project from the command-line by invoking `csc.exe` directly first, and then try with `msbuild`. You may have other properties in your `.csproj` file that are overwriting them or VS may be using a different compiler instance (e.g. if you're using a compiler imported via NuGet instead of the VS2019 compiler). – Dai Jun 15 '20 at 23:39
 - 
                    
 - 
                    @JerzyGrzelec I removed the `csc langversion` part of my answer because it was inaccurate and misleading. – Dai Jun 15 '20 at 23:48
 - 
                    2@JerzyGrzelec this does work. Most of the C# 9 features aren't released yet though. Have you tried using any of the features that *are* available? Have you tried installing one of the Preview versions of Visual Studio 2019? Records aren't released though, even in the latest preview – Panagiotis Kanavos Jun 25 '20 at 06:59
 - 
                    Records work in LINQPad 6 beta, but I think that one downloads the roslyn preview assemblies, and that the Visual Studio preview has more of a release cycle that is lagging slightly behind. – Lasse V. Karlsen Jun 26 '20 at 09:37
 - 
                    Not any more - .NET 5 Preview 6 was release yesterday with record, init support – Panagiotis Kanavos Jun 26 '20 at 12:10
 - 
                    Unfortunately I got the same issue. I installed .NET 5 Preview 6 (x64) on an up-to-date VS2019 (16.6.3). My langversion is set to `preview` and the compiler even tells me that it is running on a preview version. Tried some features like init and records, but only got syntax errors. – PuerNoctis Jul 01 '20 at 21:58
 - 
                    Ok, I had to use Preview 8 from the development branch and `dotnet build` directly. Using it out of VS2019 did not work for me sadly. – PuerNoctis Jul 01 '20 at 22:17
 - 
                    
 - 
                    @AnuViswan I have the preview from https://aka.ms/dotnet/net5/dev/Sdk/dotnet-sdk-win-x64.exe. The only thing that still is a little bit awry, is that I have to use `dotnet build` on the .csproj directly from CLI. Building out of VS2019, even with the "Use .NET Core Preview SDK" setting enabled, didn't work. – PuerNoctis Jul 06 '20 at 09:36
 - 
                    @PuerNoctis Thanks, finally managed get it running. Interesting, I was able to do build within VS2019. I am running VS2019 Preview , Version 16.7 Preview 3.1 – Anu Viswan Jul 06 '20 at 10:47
 
As per October 2020,
- you can explicitly use the 9.0 language version in .csproj
 - 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.
- 21,465
 - 8
 - 65
 - 74
 
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>
- 290
 - 3
 - 8