I'd like to use C# 8.0 (especially ranges and non-nullable reference types) in Visual Studio 2017. Is it possible?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
You can Use Microsoft.Net.Compilers.Toolset instead of
Microsoft.Net.Compilers
in vs2017This package is intended as a replacement for Microsoft.Net.Compilers (which is a Windows-only package) and Microsoft.NETCore.Compilers. Those packages are now deprecated and will be deleted in the future.
The package requires MSBuild 15.0 and either .NET Destkop 4.7.2 or .NET Core 2.1
The package versions:
The package version map the capability of c# 8 support compared to vs2019.
How to use
Add these lines to the .csproj
You can also use C# 8 in any .NET Project in Visual Studio 2019. https://dirkstrauss.com/enabling-c-8-in-visual-studio-2019/
There are some limitations though, but it is possible.
So the current info is:
From this, I deduce that C# 8 is currently not available in VS2017, and that there are no plans to change that.
Going forward, Microsoft want to tie C# language versions more closely to framework versions than they have in the past. They really only want you to be using C# 8 with .NET Core 3.x and .NET Standard 2.1 projects, and that means using Visual Studio 2019. My answer to Does C# 8 support the .NET Framework? has all the gory details.
However, if you really want to you can now use C# 8 in Visual Studio 2017 by using the same trick that brings C# 7 to Visual Studio 2015: install the latest version of the Microsoft.Net.Compilers Nuget package into the project. It works, but of course VS 2017 doesn't know about C# 8 syntax so it doesn't look very pretty. Here's a screenshot showing that VS 2017 is able to compile a small test library using nullable reference types and a static local method (both of which are C# 8 features):
Here's the .csproj and code if you want to try it:
-
Add on to series0ne's comment about the major releases: It is true that new VS usually ship with new C# version. However, in the past experience, it is possible to upgrade previous version of VS to compiler newer version of C# code, mainly by upgrading the "Microsoft.Net.Compilers" Nuget package. You may reference to this post for more information. How to use c#7 with Visual Studio 2015?