Visual Studio 建议使用 C# 8.0 功能,但编译器会产生错误

404*_*404 5 .net-4.6 c#-8.0 visual-studio-2019

在我的项目中,Visual Studio 建议我在 Framework 4.6 项目中使用 C# 8.0 功能,尽管 Microsoft 文档中指定的默认值是 7.3。其中一项建议的示例:

var i1 = "";
var i2 = "";
i1 = i1 ?? i2; //suggests i1 ??= i2;
Run Code Online (Sandbox Code Playgroud)

建议示例

如果我让 VS 纠正建议,IDE 不会显示任何错误或警告,但在构建时我会收到编译器错误。

我知道我可以更改langversion.csproj 文件中的 使其正常工作,但这不是重点。

为什么我会收到建议,然后继续编译直到没有错误?

langversion在我的记忆中,如果我想使用该版本的功能,我过去常常收到将项目自动升级到 7.X 功能的建议。

.csproj 文件之一的示例:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{00000000-0000-0000-0000-000000000000}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>MyProject</RootNamespace>
    <AssemblyName>MyProject</AssemblyName>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  //... References & compilation files
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Run Code Online (Sandbox Code Playgroud)

小智 0

更新Microsoft.Net.Compilers NuGet 包对我有用。

我从2.10.0更新到3.11.0。