更新时间:2023-02-11 14:38:30
与过去相比,Microsoft希望将C#语言版本与框架版本更紧密地联系在一起.他们真的只希望您将C#8与.NET Core 3.x和.NET Standard 2.1项目一起使用,这意味着使用Visual Studio2019.我对
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.
但是,如果您真的想现在可以通过使用将C#7带入Visual Studio 2015的相同技巧:安装最新版本的将Microsoft.Net.Compilers Nuget程序包放入项目.它可以工作,但是VS 2017当然不了解C#8语法,因此看起来不太漂亮.这是一个截图,显示VS 2017能够使用可为空的引用类型和静态本地方法(两者均为C#8功能)来编译小型测试库:
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):
如果要尝试,请使用.csproj和代码:
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;
}
}
}