c# - 如何更改所有项目的 Visual Studio 2017 默认语言版本?

标签 c# visual-studio

如何在C#7.0上为所有项目设置语言版本?

Project/Properties/Build/Advanced.../Advanced Build Settings screen

默认参数从哪里来,我想改默认值

PS:我指的不是UI语言

最佳答案

default的含义值来自 C# 编译器本身。所以为了改变它意味着你需要改变编译器。

但在 Visual Studio 2017 附带的编译器中 default实际上意味着 C# 7.0,因此您无需执行任何操作。

Visual Studio Project System 仅将语言版本值传递给 MSBuild。 MSBuild 将其作为 /langversion 进一步传递给 C# 编译器。选项。 /langversion选项允许您指定编译器接受的上层语言版本。换句话说,它允许您将语言功能的使用限制为特定版本。如果您使用高于您指定的语言版本的功能,C# 编译器将发出错误。就这样。如果指定 /langversion作为default , C# 编译器将接受最后一个主要 C# 语言版本包含的所有有效语法(请参阅 MSDN 上的 /langversion (C# Compiler Options) 页)。 Visual Studio 2017 附带的最后一个主要 C# 版本是 7.0。看 Features Added in C# Language Versions C# GitHub 存储库上的页面。

如果您需要启用最新次要版本(7.1、7.2 等)的功能或禁止同时为多个项目或解决方案使用某些新的 C# 功能,您可以使用 MSBuild 15 Directory.Build.props 自定义文件。相关摘录自 Customize your build MSDN 上的文章:

...now you can add a new property to every project in one step by defining it in a single file called Directory.Build.props at the root of your repo. When MSBuild runs, Microsoft.Common.props searches your directory structure for the Directory.Build.props file (and Microsoft.Common.targets looks for Directory.Build.targets). If it finds one, it imports the property. Directory.Build.props is a user-defined file that provides customizations to projects under a directory.

以下 Directory.Build.props 文件示例指示 C# 编译器在所有项目中接受最新次要 C# 版本(Visual Studio 2017 版本 15.5.3 中的 C# 7.2)的所有有效语法,因为它们的 .csproj 文件没有包括 <LangVersion>优先的标签:

<Project>
    <PropertyGroup>
        <LangVersion>latest</LangVersion>
    </PropertyGroup>
</Project>

更多信息请查看:

关于c# - 如何更改所有项目的 Visual Studio 2017 默认语言版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44699125/

相关文章:

c# - 在 C# 中删除列表列表中的元素时出现问题

c# - 聚合 C# 数据集时,如何将 DataSet 转换为 .Load() 的 DataTable 数组?

sql-server - 部署 VS 数据库项目 (DACPAC) 时处理 SQL 登录名/密码

visual-studio - 在 Visual Studio 的解决方案资源管理器窗口中一次折叠所有文件

c++ - 如何在 Visual C++ 中构建导入库 (.lib) 和 DLL?

c# - 如何在我的 WinCE 应用程序中集成扫描条码选项?

c# - 如何使用反射获取在基类中声明的方法?

c# - CSV 字符串到 DataTable

c# - 从剪贴板读取文本

c++ - _itoa 和 itoa 有什么区别?