How can I use C# 8 with Visual Studio 2017?

落爺英雄遲暮 提交于 2019-12-18 05:28:11

问题


I'd like to use C# 8.0 (especially ranges and non-nullable reference types) in Visual Studio 2017. Is it possible?


回答1:


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:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
    <LangVersion>8.0</LangVersion>    
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Net.Compilers" Version="3.3.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
  </ItemGroup>
</Project>

-

using System;

namespace CSharp8Test
{
    public class Class1
    {
        public string? NullableString { get; } = "Test";

        public static void Test()
        {
            Console.WriteLine(Test2());
            static int Test2() => 5;
        }
    }
}



回答2:


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?




回答3:


So the current info is:

  • Visual Studio 2019 has been released; it's no longer in preview.
  • C# 8 is still in preview (source).
  • The C# 8 beta compiler is available with Visual Studio 2019, or the latest .NET Core 3.0 preview. (source)

From this, I deduce that C# 8 is currently not available in VS2017, and that there are no plans to change that.




回答4:


You can Use Microsoft.Net.Compilers.Toolset instead of Microsoft.Net.Compilers in vs2017

This 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.

  • Version 3.0 includes a preview of C# 8.0 (Visual Studio 2019 version 16.0), but 2.11 was used for preview1.
  • Version 3.1 includes a preview of C# 8.0 (Visual Studio 2019 version 16.1)
  • Version 3.2 includes a preview of C# 8.0 (Visual Studio 2019 version 16.2)
  • Version 3.3 includes C# 8.0 (Visual Studio 2019 version 16.3, .NET Core 3.0)

How to use

Add these lines to the .csproj

 <PropertyGroup>
     //....
    <LangVersion>8.0</LangVersion>    
    <Nullable>enable</Nullable> <!-- to support nullable reference type -->
  </PropertyGroup>

<ItemGroup>   
    <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.3.1" PrivateAssets="All" />
  </ItemGroup>


来源:https://stackoverflow.com/questions/54701377/how-can-i-use-c-sharp-8-with-visual-studio-2017

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!