5

I followed this answer: How to enable C# 9.0-preview to enable C# 9.

I installed .NET 5 preview 5 which includes the new C# 9.

Microsoft.NETCore.App 5.0.0-preview.5.20278.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

I try to use C# 9:

public class Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }
}

with a project setting like:

<LangVersion>9</LangVersion>

but I get a compilation error:

Error CS1617 Invalid option '9' for /langversion. Use '/langversion:?' to list supported values.

I didn't find 9 in the list when run:

csc -langversion:?

The list is:

default
1
2
3
4
5
6
7.0
7.1
7.2
7.3
8.0 (default)
latestmajor
preview
latest

Then I used as suggested by this answer, but it can't help and also this.

<LangVersion>preview</LangVersion>

But I get a compilation error.

Program.cs(26,40): error CS1014: A get or set accessor expected

What did I miss to do to use C# 9 in .NET 5 preview 5?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • 1
    Does this answer your question? [How to enable C# 9.0-preview](https://stackoverflow.com/questions/62398572/how-to-enable-c-sharp-9-0-preview) – Iliar Turdushev Jun 22 '20 at 20:54
  • No, it didn't help, and no mention for c#9 setting. I listed lang versions as seen by csc. – M.Hassan Jun 22 '20 at 21:02
  • 2
    Could you try another feature of `C#9` (for example, [Pattern Matching in C# 9.0](https://github.com/dotnet/csharplang/issues/2850) or [Target-typed `new` expression](https://github.com/dotnet/csharplang/issues/100)) with this [solution](https://stackoverflow.com/a/62398665/12833205). It seems that most of the features are in progress, and only some of them merged into the `preview` branch. Feature statuses can be tracked [here](https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md). – Iliar Turdushev Jun 23 '20 at 08:05
  • @IliarTurdushev, that's what it is, I also tried using random c# 9 features, but they are not all included in this preview, tried the "Target-typed `new` expression" and that worked. – vocero Jun 23 '20 at 10:53
  • @Iliar Turdushev, it seems that majority of c#9 are in [progress](https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md) and have to wait to preview7 at least. – M.Hassan Jun 23 '20 at 13:51
  • @M.Hassan the duplicate is actually correct. If you want to use a preview language version, you set `preview` in the LangVersion tag. In fact, you don't even need to set `preview` - it's the default choice when using preview .NET Core versions. Very few C# 9 features are available in Preview 5 though. – Panagiotis Kanavos Jun 25 '20 at 06:55
  • @M.Hassan a better question would be how to use specific C# 9 features, assuming they are released – Panagiotis Kanavos Jun 25 '20 at 06:55
  • 1
    @M.Hassan the .NET Preview 6 that was released just yesterday allows the use of eg records, init and other features. You don't have to do anything to enable them, just set the language version to `preview` – Panagiotis Kanavos Jun 26 '20 at 12:07
  • @Panagiotis Kanavos, good news and valuable, I'll try it and feedback. – M.Hassan Jun 26 '20 at 14:02
  • @PanagiotisKanavos,I installed preview6 and set the language to preview. It seems that it's not supported yet, I get error: `error CS0518: Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported`. Do I miss something? – M.Hassan Jun 26 '20 at 15:04
  • @PanagiotisKanavos, I modified the title of the question, and record feature is working. – M.Hassan Jun 26 '20 at 16:44

2 Answers2

7

I enabled C# 9 as described in this answer and @PanagiotisKanavos:

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>net5.0</TargetFramework>
  <LangVersion>Preview</LangVersion>
</PropertyGroup>

.NET 5 Preview 5 or Preview 6 don't support the init feature and majority of C# 9.

I installed latest SDK bits of development from https://aka.ms/dotnet/net5/dev/Sdk/dotnet-sdk-win-x64.exe.

Currently preview 8 is available (version 5.0.100-preview.8.20327.5).

I can build projects including init feature in Visual Studio 2019.6.2 (the editor still show red lines, but build success) or using Visual Studio Code (perfect, and IDE sense the new C# 9 syntax).

Most, almost all, features of C# 9 in the blog post are working fine with .NET 5 Preview 8.

Update July 3, 2020

Alternative Solution

For working with .NET 5 Preview 5, install the package Microsoft.Net.Compilers.Toolset, Version 3.8.0-1.20330.5. It support init feature and many c# 9 new features.

Install-Package Microsoft.Net.Compilers.Toolset -Version 3.8.0-1.20330.5 -Source https://dotnet.myget.org/F/roslyn/api/v3/index.json

Update Aug 7, 2020

Visual Studio 2019 version 16.7 is released with support C# 9.

In developer Command Prompt, type the command:

csc -langversion:?

Supported language versions:
default
1
2
3
4
5
6
7.0
7.1
7.2
7.3
8.0 (default)
9.0
latestmajor
preview
latest

You see 9.0 in the list above, and you can define LangVersion as 9.0:

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>net5.0</TargetFramework>
  <LangVersion>9.0</LangVersion>
</PropertyGroup>

You need not to install Microsoft.Net.Compilers.Toolset v 3.8.0-1.20330.5 because C# 9 is supported.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • For me it works with preview 8 and invoking `dotnet build` directly from CLI. Building from VS2019 gave me compiler errors. May you have a clue why? – PuerNoctis Jul 01 '20 at 22:18
  • Version is 16.6.3, and using `init` it gives me the message `error CS1014: A get or set accessor expected`. MSBuild output tells me that the preview version of .NET is used, but maybe it does not recognize the preview SDK for .NET 5? As I said, the same code compiles when running the `dotnet build` on the .csproj file without any alterations or command line switches, it's weird. – PuerNoctis Jul 02 '20 at 08:10
  • 1
    Oh, and I have the 'Use preview featuers of .NET SDK' in the settings of VS checked. – PuerNoctis Jul 02 '20 at 08:11
1

Following Getting Setup With C# 9 Preview:

There are two main things specific to Visual Studio.

  • Ensure that Visual Studio is updated to the very latest version. I cannot tell you how many times things don't work in Visual Studio, but do work from the command line or Visual Studio Code, and it's because of the version of Visual Studio.

  • Secondly, you need to go menu ToolsOptions → "Preview Features" and then tick the box that says "Use previews of the .NET Core SDK". I think they added this feature so that you could "play" with the preview SDK's, but not have your everyday work being built by preview features. But you need it ticked for things to work.

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MindingData
  • 11,924
  • 6
  • 49
  • 68