选择 Visual Basic 语言版本Select the Visual Basic language version

Visual Basic 编译器默认为已发布语言的最新主版本。The Visual Basic compiler defaults to the latest major version of the language that has been released. 你可以选择使用该语言的新单点版本编译任何项目。You may choose to compile any project using a new point release of the language. 选择语言的较新版本,让你的项目可以使用最新的语言功能。Choosing a newer version of the language enables your project to make use of the latest language features. 在其他情况下,可能需要在使用较旧的语言版本时验证项目的编译内容是否整齐。In other scenarios, you may need to validate that a project compiles cleanly when using an older version of the language.

借助此功能,在开发环境中安装新版本的 SDK 和工具时,不必选择在项目中引入新的语言功能。This capability decouples the decision to install new versions of the SDK and tools in your development environment from the decision to incorporate new language features in a project. 可以在生成计算机上安装最新的 SDK 和工具。You can install the latest SDK and tools on your build machine. 每个项目可以配置为对其生成使用该语言的特定版本。Each project can be configured to use a specific version of the language for its build.

有三种方法可设置语言版本:There are three ways to set the language version:

编辑 .vbproj 文件Edit the vbproj file

您可以在 .vbproj 文件中设置语言版本。You can set the language version in your .vbproj file. 添加以下元素:Add the following element:

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

该值 latest 使用 Visual Basic 语言的最新次要版本。The value latest uses the latest minor version of the Visual Basic language. 有效值是:Valid values are:

Value 含义Meaning
defaultdefault 编译器接受它可支持的最新主版本中的所有有效语言语法。The compiler accepts all valid language syntax from the latest major version that it can support.
99 编译器仅接受 Visual Basic 9.0 或更低版本中所含的语法。The compiler accepts only syntax that is included in Visual Basic 9.0 or lower.
1010 编译器仅接受 Visual Basic 10.0 或更低版本中所含的语法。The compiler accepts only syntax that is included in Visual Basic 10.0 or lower.
1111 编译器仅接受 Visual Basic 11.0 或更低版本中所含的语法。The compiler accepts only syntax that is included in Visual Basic 11.0 or lower.
1212 编译器仅接受 Visual Basic 12.0 或更低版本中所含的语法。The compiler accepts only syntax that is included in Visual Basic 12.0 or lower.
1414 编译器仅接受 Visual Basic 14.0 或更低版本中所含的语法。The compiler accepts only syntax that is included in Visual Basic 14.0 or lower.
1515 编译器仅接受 Visual Basic 15.0 或更低版本中所含的语法。The compiler accepts only syntax that is included in Visual Basic 15.0 or lower.
15.315.3 编译器仅接受 Visual Basic 15.3 或更低版本中所含的语法。The compiler accepts only syntax that is included in Visual Basic 15.3 or lower.
15.515.5 编译器仅接受 Visual Basic 15.5 或更低版本中所含的语法。The compiler accepts only syntax that is included in Visual Basic 15.5 or lower.
1616 编译器仅接受 Visual Basic 16 或更低版本中包含的语法。The compiler accepts only syntax that is included in Visual Basic 16 or lower.
16.916.9 编译器仅接受 Visual Basic 16.9 或更低版本中所含的语法。The compiler accepts only syntax that is included in Visual Basic 16.9 or lower.
最新latest 编译器接受它可支持的所有有效语言语法。The compiler accepts all valid language syntax that it can support.

特殊字符串 defaultlatest 分别解析为生成计算机上安装的最新主要和次要语言版本。The special strings default and latest resolve to the latest major and minor language versions installed on the build machine, respectively.

配置多个项目Configure multiple projects

你可以创建一个 属性 文件,其中包含 <LangVersion> 用于配置多个目录的元素。You can create a Directory.build.props file that contains the <LangVersion> element to configure multiple directories. 通常是在解决方案目录中完成这件事。You typically do that in your solution directory. 将以下内容添加到你的解决方案目录中的 属性 文件:Add the following to a Directory.build.props file in your solution directory:

<Project>
 <PropertyGroup>
   <LangVersion>15.5</LangVersion>
 </PropertyGroup>
</Project>

现在,包含该文件的目录的每个子目录中的生成将使用 Visual Basic 版本15.5 语法。Now, builds in every subdirectory of the directory containing that file will use Visual Basic version 15.5 syntax. 有关详细信息,请参阅关于自定义生成的文章。For more information, see the article on Customize your build.

选择 langversion 编译器选项Set the langversion compiler option

你可以使用 -langversion 命令行选项。You can use the -langversion command-line option. 有关详细信息,请参阅关于 /langversion 编译器选项的文章。For more information, see the article on the -langversion compiler option. 您可以通过键入来查看有效值的列表 vbc -langversion:?You can see a list of the valid values by typing vbc -langversion:? .