功能“使用声明”在 C# 7.3 中不可用。请使用 8.0 或更高版本的语言 - 在一台机器上出错,但在另一台机器上工作

Ogg*_*las 83 c# .net-4.6

Visual Studio Enterprise 16.3.7在两台单独的机器上使用时,一台机器构建良好,另一台机器抛出错误:

功能“使用声明”在 C# 7.3 中不可用。请使用 8.0 或更高版本的语言。

在此处输入图片说明

在此处输入图片说明

这可以通过按照此处的建议设置LangVersion在非工作机器上轻松解决/sf/answers/3365990281/或让 Visual Studio 像上面的打印屏幕一样自动修复它。.csproj

<LangVersion>8.0</LangVersion>
Run Code Online (Sandbox Code Playgroud)

我不明白的是为什么一台机器在没有这条线的情况下可以很好地构建而.csproj另一台机器需要它?

Jam*_*rti 94

我收到了同样的错误,但我只是忘记包括

<LangVersion>8.0</LangVersion>
Run Code Online (Sandbox Code Playgroud)

解决方案中所有.csproj 文件中的属性。以下是我当前的 c# 8 设置:

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
    <NullableContextOptions>enable</NullableContextOptions>
  </PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

我发现以下文档在从核心 2.2 迁移到 3.x 时最有帮助:

MSDN 2.2 -> 3.0

MSDN 3.0 -> 3.1

  • 您可以使用 `&lt;LangVersion&gt;latest&lt;/LangVersion&gt;` 而不是 `&lt;LangVersion&gt;8.0&lt;/LangVersion&gt;` (22认同)
  • 另外,请确保将 `&lt;LangVersion`&gt; 放在 `&lt;PropertyGroup&gt;` 内,而不是放在 `&lt;PropertyGroup Condition=" '$(Configuration)|$(Platform)' == '&lt;some certain config&gt;' "&gt;` 内 (6认同)
  • 我认为第一个链接 `MSDN 2.2 -&gt; 3.0` 旨在链接到以下内容:https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30 我尝试过编辑修改它的答案,但是“建议的编辑队列已满”,所以将在这里发布。 (2认同)

Eli*_*ron 65

这可能是因为编译器默认为不同的目标框架使用不同的 C# 语言版本。

要覆盖默认的 C# 语言,请添加到项目文件(如有问题的建议):

<PropertyGroup>
   <LangVersion>8.0</LangVersion>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

或者:

<PropertyGroup>
   <LangVersion>latest</LangVersion>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

有关不同目标框架的默认 C# 语言版本以及如何手动选择C# 语言版本,请参阅C# 语言版本控制

另请参阅堆栈溢出答案Does C# 8 support the .NET Framework?有关此主题的更多信息。


这是C# 语言版本控制文章的一部分:

C# 语言版本控制

最新的 C# 编译器根据项目的目标框架或框架确定默认语言版本。这是因为 C# 语言可能具有依赖类型或运行时组件的功能,而这些功能在每个 .NET 实现中都不可用。这也确保对于您的项目所针对的任何目标,默认情况下您都会获得最高兼容的语言版本。

本文中的规则适用于 Visual Studio 2019 或 .NET Core 3.0 SDK 提供的编译器。默认情况下,作为 Visual Studio 2017 安装或更早 .NET Core SDK 版本一部分的 C# 编译器面向 C# 7.0。

默认值

编译器根据以下规则确定默认值:

????????????????????????????????????????????????????????????
? Target framework ? version ? C# language version default ?
????????????????????????????????????????????????????????????
? .NET Core        ? 3.x     ? C# 8.0                      ?
? .NET Core        ? 2.x     ? C# 7.3                      ?
? .NET Standard    ? 2.1     ? C# 8.0                      ?
? .NET Standard    ? 2.0     ? C# 7.3                      ?
? .NET Standard    ? 1.x     ? C# 7.3                      ?
? .NET Framework   ? all     ? C# 7.3                      ?
????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

覆盖默认值

如果必须显式指定 C# 版本,可以通过多种方式执行此操作:

  • 手动编辑您的项目文件。
  • 为子目录中的多个项目设置语言版本。
  • 配置 -langversion 编译器选项。

编辑项目文件

您可以在项目文件中设置语言版本。例如,如果您明确想要访问预览功能,请添加如下元素:

<PropertyGroup>
   <LangVersion>preview</LangVersion>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

该值preview使用您的编译器支持的最新可用预览 C# 语言版本。

配置多个项目

要配置多个项目,您可以创建一个包含该元素的Directory.Build.props文件<LangVersion>。您通常在解决方案目录中执行此操作。将以下内容添加到解决方案目录中的Directory.Build.props文件中:

<Project>
 <PropertyGroup>
   <LangVersion>preview</LangVersion>
 </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

现在,在包含该文件的目录的每个子目录中构建将使用预览 C# 版本。有关更多信息,请参阅关于自定义您的构建的文章。

C#语言版本参考

下表显示了所有当前的 C# 语言版本。如果较旧,您的编译器可能不一定理解每个值。如果您安装 .NET Core 3.0 或更高版本,则您可以访问列出的所有内容。

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? Value                 ? Meaning                                                                                                  ?
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? preview               ? The compiler accepts all valid language syntax from the latest preview version.                          ?
? latest                ? The compiler accepts syntax from the latest released version of the compiler (including minor version).  ?
? latestMajor (default) ? The compiler accepts syntax from the latest released major version of the compiler.                      ?
? 8.0                   ? The compiler accepts only syntax that is included in C# 8.0 or lower.                                    ?
? 7.3                   ? The compiler accepts only syntax that is included in C# 7.3 or lower.                                    ?
? 7.2                   ? The compiler accepts only syntax that is included in C# 7.2 or lower.                                    ?
? 7.1                   ? The compiler accepts only syntax that is included in C# 7.1 or lower.                                    ?
? 7                     ? The compiler accepts only syntax that is included in C# 7.0 or lower.                                    ?
? 6                     ? The compiler accepts only syntax that is included in C# 6.0 or lower.                                    ?
? 5                     ? The compiler accepts only syntax that is included in C# 5.0 or lower.                                    ?
? 4                     ? The compiler accepts only syntax that is included in C# 4.0 or lower.                                    ?
? 3                     ? The compiler accepts only syntax that is included in C# 3.0 or lower.                                    ?
? ISO-2 (or 2)          ? The compiler accepts only syntax that is included in ISO/IEC 23270:2006 C# (2.0).                        ?
? ISO-1 (or 1)          ? The compiler accepts only syntax that is included in ISO/IEC 23270:2003 C# (1.0/1.2).                    ?
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)


小智 22

我不得不将 Visual Studio 版本从 16.3.X 更新到 16.4.2。这解决了问题,我不必添加任何 LangVersion。

学分:https : //github.com/aspnet/AspNetCore.Docs/issues/16047


小智 9

我下载了最新版本的 .Net Core 3.0 和 3.1 并遇到了同样的问题。对我来说,修复程序似乎下载了 Visual Studio 2019 的最新更新(版本 16.4.2)。

这也重新启动了我的计算机,错误消失了。


Sab*_*ncu 8

2021年

原因:您的目标可能是.NET Standard 2.0,它使用C# 7.3

修复:在项目的属性下,单击应用程序面板并选择.NET Standard 2.1作为目标框架

进行上述更改后,Visual Studio 2019 将自行修复该问题,无需LangVersion进行任何设置。

请参阅:C# 语言版本控制

在此输入图像描述