Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling C# 9 in .NET 5 Preview 5 raises a compiler error for the new C# 9 feature

Tags:

c#

.net-5

c#-9.0

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?

like image 212
M.Hassan Avatar asked Jun 22 '20 18:06

M.Hassan


People also ask

How do I enable C++ in Visual Studio Code?

C/C++ for Visual Studio Code. C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS. Install the extension. Open VS Code. Select the Extensions view icon on the Activity bar or use the keyboard shortcut (⇧⌘X (Windows, Linux Ctrl+Shift+X)).

Should you enable C #8 in your project?

Should you Enable C# 8? Enabling C# 8 in any .NET Project is super easy. If you can live with the limitations and exclusions of some types, there is no reason why you shouldn’t try using C# 8. If you are not familiar with all the features of C# 8, then have a look at the following list.

How do I enable cross-platform C/C++ support for Visual Studio Code?

C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS. Open VS Code. Select the Extensions view icon on the Activity bar or use the keyboard shortcut ( Ctrl+Shift+X ).

How to enable Ctrl + C / Ctrl + V in Windows?

1. Using Command Prompt Settings. It’s quite easy to enable Ctrl + C / Ctrl + V in Windows command prompt. All you need to do is just follow the below simple steps. Open the Run command in your PC by using the Window + R keyboard shortcut keys. Type cmd in the text box and hit the enter key to launch command prompt.


Video Answer


2 Answers

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.

like image 54
M.Hassan Avatar answered Oct 30 '22 22:10

M.Hassan


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

like image 23
MindingData Avatar answered Oct 30 '22 23:10

MindingData