.NET Core 2.0 的新增功能What's new in .NET Core 2.0

.NET Core 2.0 提供以下几个方面的增强功能和新功能:.NET Core 2.0 includes enhancements and new features in the following areas:

工具Tooling

dotnet restore 隐式运行dotnet restore runs implicitly

在旧版 .NET Core 中,在使用 dotnet new 命令新建项目后,以及每当向项目添加新的依赖项时,都必须立即运行 dotnet restore 命令,以便下载依赖项。In previous versions of .NET Core, you had to run the dotnet restore command to download dependencies immediately after you created a new project with the dotnet new command, as well as whenever you added a new dependency to your project.

备注

从 .NET Core 2.0 SDK 开始,无需运行 dotnet restore,因为它由所有需要还原的命令隐式运行,如 dotnet newdotnet builddotnet runStarting with .NET Core 2.0 SDK, you don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet new, dotnet build and dotnet run. 在执行显式还原有意义的某些情况下,例如 Azure DevOps Services 中的持续集成生成中,或在需要显式控制还原发生时间的生成系统中,它仍然是有效的命令。It's still a valid command in certain scenarios where doing an explicit restore makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control the time at which the restore occurs.

还可以将 --no-restore 开关传递到 newrunbuildpublishpacktest 命令,从而禁用自动调用 dotnet restoreYou can also disable the automatic invocation of dotnet restore by passing the --no-restore switch to the new, run, build, publish, pack, and test commands.

重定目标到 .NET Core 2.0Retargeting to .NET Core 2.0

如果已安装 .NET Core 2.0 SDK,那么定目标到 .NET Core 1.x 的项目可以重定目标到 .NET Core 2.0。If the .NET Core 2.0 SDK is installed, projects that target .NET Core 1.x can be retargeted to .NET Core 2.0.

若要重定目标到 .NET Core 2.0,请将 <TargetFramework> 元素(或 <TargetFrameworks> 元素,如果项目文件中有多个目标的话)值从 1.x 更改为 2.0,从而编辑项目文件:To retarget to .NET Core 2.0, edit your project file by changing the value of the <TargetFramework> element (or the <TargetFrameworks> element if you have more than one target in your project file) from 1.x to 2.0:

<PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
 </PropertyGroup>

还可以用同样的方式,将 .NET Standard 库重定目标到 .NET Standard 2.0:You can also retarget .NET Standard libraries to .NET Standard 2.0 the same way:

<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
 </PropertyGroup>

若要详细了解如何将项目迁移到 .NET Core 2.0,请参阅从 ASP.NET Core 1.x 迁移到 ASP.NET Core 2.0For more information about migrating your project to .NET Core 2.0, see Migrating from ASP.NET Core 1.x to ASP.NET Core 2.0.

语言支持Language support

除了支持 C# 和 F# 外,.NET Core 2.0 还支持 Visual Basic。In addition to supporting C# and F#, .NET Core 2.0 also supports Visual Basic.

Visual BasicVisual Basic

在版本 2.0 发布后,.NET Core 现在支持 Visual Basic 2017。With version 2.0, .NET Core now supports Visual Basic 2017. 可使用 Visual Basic 创建以下类型项目:You can use Visual Basic to create the following project types:

  • .NET Core 控制台应用程序.NET Core console apps
  • .NET Core 类库.NET Core class libraries
  • .NET Standard 类库.NET Standard class libraries
  • .NET Core 单元测试项目.NET Core unit test projects
  • .NET Core xUnit 测试项目.NET Core xUnit test projects

例如,若要创建 Visual Basic“Hello World”应用程序,请通过命令行按照以下步骤操作:For example, to create a Visual Basic "Hello World" application, do the following steps from the command line:

  1. 打开控制台窗口,创建项目目录,并将它设为当前目录。Open a console window, create a directory for your project, and make it the current directory.

  2. 输入命令 dotnet new console -lang vbEnter the command dotnet new console -lang vb.

    此命令创建文件扩展名为 .vbproj 的项目文件,以及名为 Program.vb 的 Visual Basic 源代码文件。The command creates a project file with a .vbproj file extension, along with a Visual Basic source code file named Program.vb. 此文件包含用于将字符串“Hello World!”写入控制台窗口的源代码This file contains the source code to write the string "Hello World!" to the console window.

  3. 输入命令 dotnet runEnter the command dotnet run. .NET Core CLI 自动编译并执行应用程序,在控制台窗口中The .NET Core CLI automatically compiles and executes the application, which displays the message "Hello World!" 显示文本字符串“Hello World!”。in the console window.

支持 C# 7.1Support for C# 7.1

.NET Core 2.0 支持 C# 7.1,其中新增了大量功能,具体包括:.NET Core 2.0 supports C# 7.1, which adds a number of new features, including:

  • 可以使用 async 关键字标记 Main 方法(应用程序入口点)。The Main method, the application entry point, can be marked with the async keyword.
  • 推断的元组名称。Inferred tuple names.
  • 默认表达式。Default expressions.

平台改进Platform improvements

.NET Core 2.0 包括许多功能,可便于用户更轻松地在支持的操作系统上安装并使用 .NET Core。.NET Core 2.0 includes a number of features that make it easier to install .NET Core and to use it on supported operating systems.

.NET Core for Linux 是一个实现代码.NET Core for Linux is a single implementation

.NET Core 2.0 提供一个 Linux 实现代码,适用于多个 Linux 发行版本。.NET Core 2.0 offers a single Linux implementation that works on multiple Linux distributions. .NET Core 1.x 要求下载发行版本专属的 Linux 实现代码。.NET Core 1.x required that you download a distribution-specific Linux implementation.

还可以开发定目标到 Linux 一个操作系统的应用程序。You can also develop apps that target Linux as a single operating system. .NET Core 1.x 要求分别定目标到每个 Linux 发行版本。.NET Core 1.x required that you target each Linux distribution separately.

支持 Apple 加密库Support for the Apple cryptographic libraries

macOS 上的 .NET Core 1.x 要求使用 OpenSSL 工具包的加密库。.NET Core 1.x on macOS required the OpenSSL toolkit's cryptographic library. .NET Core 2.0 使用 Apple 加密库,不要求使用 OpenSSL,因此不再需要安装它。.NET Core 2.0 uses the Apple cryptographic libraries and doesn't require OpenSSL, so you no longer need to install it.

API 更改和库支持API changes and library support

支持 .NET Standard 2.0Support for .NET Standard 2.0

.NET Standard 定义了一组版本化 API,这些 API 必须可用于符合相应 Standard 版本要求的 .NET 实现代码。The .NET Standard defines a versioned set of APIs that must be available on .NET implementations that comply with that version of the standard. .NET Standard 面向库开发者。The .NET Standard is targeted at library developers. 旨在保证功能对每个 .NET 实现代码中定目标到 .NET Standard 版本的库可用。It aims to guarantee the functionality that is available to a library that targets a version of the .NET Standard on each .NET implementation. .NET Core 1.x 支持 .NET Standard 版本 1.6;.NET Core 2.0 支持最新版 .NET Core 2.0。.NET Core 1.x supports the .NET Standard version 1.6; .NET Core 2.0 supports the latest version, .NET Standard 2.0. 有关详细信息,请参阅 .NET StandardFor more information, see .NET Standard.

.NET Standard 2.0 比 .NET Standard 1.6 多包含 20,000 多个 API。.NET Standard 2.0 includes over 20,000 more APIs than were available in the .NET Standard 1.6. 此扩展的外围应用的大部分来自于,将 .NET Framework 和 Xamarin 的通用 API 合并到 .NET Standard。Much of this expanded surface area results from incorporating APIs that are common to the .NET Framework and Xamarin into .NET Standard.

.NET Standard 2.0 类库还可以引用 .NET Framework 类库,但前提是它们调用 .NET Standard 2.0 中的 API。.NET Standard 2.0 class libraries can also reference .NET Framework class libraries, provided that they call APIs that are present in the .NET Standard 2.0. 不需要重新编译 .NET Framework 库。No recompilation of the .NET Framework libraries is required.

有关自上一版本 (.NET Standard 1.6) 起添加到 .NET Standard 的 API 列表,请参阅 .NET Standard 2.0 与 1.6For a list of the APIs that have been added to the .NET Standard since its last version, the .NET Standard 1.6, see .NET Standard 2.0 vs. 1.6.

扩展的外围应用Expanded surface area

与 .NET Core 1.1 相比,.NET Core 2.0 中的可用 API 总数增加了一倍以上。The total number of APIs available on .NET Core 2.0 has more than doubled in comparison with .NET Core 1.1.

借助 Windows 兼容包,从 .NET Framework 移植也简单了很多。And with the Windows Compatibility Pack porting from .NET Framework has also become much simpler.

支持 .NET Framework 库Support for .NET Framework libraries

.NET Core 代码可以引用现有的 .NET Framework 库,包括现有的 NuGet 包。.NET Core code can reference existing .NET Framework libraries, including existing NuGet packages. 请注意,库必须使用 .NET Standard 中的 API。Note that the libraries must use APIs that are found in .NET Standard.

Visual Studio 集成Visual Studio integration

Visual Studio 2017 版本 15.3 和(在某些情况下)Visual Studio for Mac 提供了大量面向 .NET Core 开发者的重大增强功能。Visual Studio 2017 version 15.3 and in some cases Visual Studio for Mac offer a number of significant enhancements for .NET Core developers.

重定目标 .NET Core 应用程序和 .NET Standard 库Retargeting .NET Core apps and .NET Standard libraries

如果已安装 .NET Core 2.0 SDK,可以将 .NET Core 1.x 项目重定目标到 .NET Core 2.0,并将 .NET Standard 1.x 库重定目标到 .NET Standard 2.0。If the .NET Core 2.0 SDK is installed, you can retarget .NET Core 1.x projects to .NET Core 2.0 and .NET Standard 1.x libraries to .NET Standard 2.0.

若要在 Visual Studio 中重定目标项目,可以打开项目属性对话框的“应用程序” 选项卡,再将“目标框架” 值更改为“.NET Core 2.0” 或“.NET Standard 2.0” 。To retarget your project in Visual Studio, you open the Application tab of the project's properties dialog and change the Target framework value to .NET Core 2.0 or .NET Standard 2.0. 还可以通过右键单击项目并选择“编辑 *.csproj 文件” 选项进行更改。You can also change it by right-clicking on the project and selecting the Edit *.csproj file option. 有关详细信息,请参阅本主题前面的工具部分。For more information, see the Tooling section earlier in this topic.

.NET Core 的 Live Unit Testing 支持Live Unit Testing support for .NET Core

修改代码时,Live Unit Testing 在后台自动运行任何受影响的单元测试,并在 Visual Studio 环境中实时显示结果和代码覆盖率。Whenever you modify your code, Live Unit Testing automatically runs any affected unit tests in the background and displays the results and code coverage live in the Visual Studio environment. .NET Core 2.0 现在支持 Live Unit Testing。.NET Core 2.0 now supports Live Unit Testing. 以前,Live Unit Testing 仅适用于 .NET Framework 应用程序。Previously, Live Unit Testing was available only for .NET Framework applications.

有关详细信息,请参阅结合使用 Live Unit Testing 和 Visual Studio 2017Live Unit Testing FAQFor more information, see Live Unit Testing with Visual Studio 2017 and the Live Unit Testing FAQ.

更好地支持多个目标框架Better support for multiple target frameworks

若要为多个目标框架生成项目,现在可以从顶级菜单中选择目标平台。If you're building a project for multiple target frameworks, you can now select the target platform from the top-level menu. 在下图中,名为 SCD1 的项目将目标定为 64 位 macOS X 10.11 (osx.10.11-x64) 和 64 位 Windows 10/Windows Server 2016 (win10-x64)。In the following figure, a project named SCD1 targets 64-bit macOS X 10.11 (osx.10.11-x64) and 64-bit Windows 10/Windows Server 2016 (win10-x64). 可以先选择目标框架,再选择项目按钮(在此示例中是为了要运行调试版本)。You can select the target framework before selecting the project button, in this case to run a debug build.

显示生成项目时的目标框架选择的屏幕截图。

对 .NET Core SDK 的并行支持Side-by-side support for .NET Core SDKs

现在可以安装 .NET Core SDK,与 Visual Studio 互不影响。You can now install the .NET Core SDK independently of Visual Studio. 这样,单版本 Visual Studio 可以生成定目标到不同 .NET Core 版本的项目。This makes it possible for a single version of Visual Studio to build projects that target different versions of .NET Core. 以前,Visual Studio 和 .NET Core SDK 紧密结合在一起;特定版本的 SDK 附带了特定版本的 Visual Studio。Previously, Visual Studio and the .NET Core SDK were tightly coupled; a particular version of the SDK accompanied a particular version of Visual Studio.

文档改进Documentation improvements

.NET 应用程序体系结构.NET Application Architecture

通过 .NET 应用程序体系结构,可以查看一系列电子图书,其中提供了有关如何使用 .NET 生成内容的指导、最佳做法和示例应用程序:.NET Application Architecture gives you access to a set of e-books that provide guidance, best practices, and sample applications when using .NET to build:

请参阅See also