.NET 示例和教程.NET samples and tutorials

.NET 文档包含指导用户如何使用 .NET 的一系列示例和教程。The .NET documentation contains a set of samples and tutorials that teach you about .NET. 本主题介绍如何查找、查看和下载 .NET Core、ASP.NET Core 和 C# 示例和教程。This topic describes how to find, view, and download .NET Core, ASP.NET Core, and C# samples and tutorials. 可在 F# 基础站点找到学习 F# 编程语言的资源。Find resources to learn the F# programming language on the F# Foundation's site. 如果想要使用联机代码编辑器来探索 C#,请先查看此交互式教程然后再继续学习 C# 交互式教程If you're interested in exploring C# using an online code editor, start with this interactive tutorial and continue with C# interactive tutorial. 有关如何查看和下载示例代码的说明,请参阅查看和下载示例部分。For instructions on how to view and download sample code, see the Viewing and downloading samples section.

.NET Core.NET Core

示例Samples

在 .NET Core 中使用 dotnet 测试的单元测试Unit Testing in .NET Core using dotnet test

本指南演示如何创建 ASP.NET Core Web 应用和关联的单元测试。This guide shows you how to create an ASP.NET Core web app and associated unit tests. 首先创建一个简单的 Web 服务应用,然后添加测试。It starts by creating a simple web service app and then adds tests. 然后继续创建更多测试来指导新功能的实现。It continues with creating more tests to guide implementing new features. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

教程Tutorials

使用 CLI 工具编写 .NET Core 控制台应用:分步指南Writing .NET Core console apps using the CLI tools: A step-by-step guide

本指南介绍如何使用 .NET Core CLI 工具生成跨平台控制台应用。This guide shows you how to use the .NET Core CLI tooling to build cross-platform console apps. 将从基本的控制台应用开始介绍,最终涵盖包括测试在内的多个项目。It starts with a basic console app and eventually spans multiple projects, including testing. 逐步添加功能,不断地增长知识。You add features step-by-step, building your knowledge as you go. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

使用跨平台工具编写库Writing Libraries with Cross Platform Tools

本示例介绍如何使用跨平台 CLI 工具编写 .NET 的库。This sample covers how to write libraries for .NET using cross-platform CLI tools. 这些工具提供可跨任何支持的操作系统工作的高效率、低级别体验。These tools provide an efficient and low-level experience that works across any supported operating system. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

ASP.NET CoreASP.NET Core

请参阅 ASP.NET Core 教程See the ASP.NET Core tutorials. ASP.NET Core 文档中的许多文章都提供了为其编写的示例的链接。Many articles in the ASP.NET Core documentation have links to samples written for them.

C# 语言C# language

示例Samples

迭代器Iterators

本示例演示用于创建和使用 C# 迭代器的语法和功能。This sample demonstrates the syntax and features for creating and consuming C# iterators. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

索引器Indexers

本示例演示用于 C# 索引器的语法和功能。This sample demonstrates the syntax and features for C# indexers. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

委托和事件Delegates and Events

本示例演示用于 C# 委托和事件的语法和功能。This sample demonstrates the syntax and features for C# delegates and events. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub. 侧重于事件的第二个示例也位于相同的存储库中。A second sample focused on events is also in the same repository.

表达式树Expression Trees

本示例演示可以使用表达式树解决的许多问题。This sample demonstrates many of the problems that can be solved by using Expression Trees. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

LINQ 示例LINQ Samples

这一系列示例将演示许多语言集成查询 (LINQ) 功能。This series of samples demonstrate many of the features of Language Integrated Query (LINQ). 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

托管 COM 服务器示例Managed COM server Sample

COM 服务器示例展示了如何创建托管 COM 服务器,以及如何通过 RegFree COM 全局注册或使用它。The COM server sample demonstrates the creation of a managed COM server and how it can be globally registered or consumed via RegFree COM.

Microsoft Office PIA 示例Microsoft Office PIA Sample

ExcelDemo 示例演示了 Microsoft Office PIA 在 .NET Core 中的使用情况。The ExcelDemo sample demonstrates the consumption of Microsoft Office PIAs in .NET Core.

教程Tutorials

控制台应用程序Console Application

本教程演示控制台 I/O、控制台应用的结构以及基于任务的异步编程模型的基础。This tutorial demonstrates Console I/O, the structure of a console app, and the basics of the task-based asynchronous programming model. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

REST 客户端REST Client

本教程演示 Web 通信、JSON 序列化和 C# 语言中面向对象的特性。This tutorial demonstrates web communications, JSON serialization, and object-oriented features of the C# language. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

使用 LINQWorking with LINQ

本教程演示许多 LINQ 功能和支持 LINQ 的语言元素。This tutorial demonstrates many of the features of LINQ and the language elements that support it. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

借助 Visual Studio for Mac 在 macOS 上开始使用 .NET CoreGetting started with .NET Core on macOS using Visual Studio for Mac

本教程演示了如何使用 Visual Studio for Mac 生成简单的 .NET Core 控制台应用。This tutorial shows you how to build a simple .NET Core console app using Visual Studio for Mac.

使用 Visual Studio for Mac 在 macOS 上构建完整的 .NET Core 解决方案Building a complete .NET Core solution on macOS using Visual Studio for Mac

本教程演示了如何构建包含可重用的库和单元测试的的完整 .NET Core 解决方案。This tutorial shows you how to build a complete .NET Core solution that includes a reusable library and unit testing.

创建支持插件的 .NET Core 应用程序Creating a .NET Core application that supports plugins

本教程演示了如何在 .NET Core 上构建支持插件基础结构的简单应用程序。This tutorial shows you how to build a simple application on .NET Core that supports a plugin architecture. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

部署到容器Deploying to containers

在 Windows Docker 容器中运行 ASP.NET MVC 应用程序Running ASP.NET MVC Applications in Windows Docker Containers

本教程演示如何在 Windows Docker 容器中部署现有 ASP.NET MVC 应用。This tutorial demonstrates how to deploy an existing ASP.NET MVC app in a Windows Docker Container. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

在 Windows 容器中运行 .NET Framework 控制台应用程序Running .NET Framework Console Applications in Windows Containers

本教程演示如何在 Windows 容器中部署现有控制台应用。This tutorial demonstrates how to deploy an existing console app in a Windows container. 可在 GitHub 上的 dotnet/samples 存储库中找到完整示例The completed sample is available in the dotnet/samples repository on GitHub.

查看和下载示例Viewing and downloading samples

许多主题显示了可从 GitHub 查看或下载的源代码和示例。Many topics show source code and samples that are available for viewing or download from GitHub. 要查看示例,只需点击示例链接即可。To view a sample, just follow the sample link. 要下载代码,请按照以下说明进行操作:To download the code, follow these instructions:

  1. 执行以下过程之一,下载包含示例代码的存储库:Download the repository that contains the sample code by performing one of the following procedures:
    • 将 ZIP 格式的存储库下载到本地系统。Download a ZIP of the repository to your local system. 解压缩压缩存档文件。Un-ZIP the compressed archive.
    • 分叉存储库并将分叉点克隆到你的本地系统。Fork the repository and clone the fork to your local system. 分叉和克隆允许通过对分叉点进行更改,然后为官方文档存储库创建提取请求的方式参与文档创作。Forking and cloning permits you to make contributions to the documentation by committing changes to your fork and then creating a pull request for the official docs repository. 有关详细信息,请参阅 .NET 文档参与指南ASP.NET 文档参与指南For more information, see the .NET Documentation Contributing Guide and the ASP.NET Docs Contributing Guide.
    • 以本地方式克隆存储库。Clone the repository locally. 如果你将文档存储库直接克隆到本地系统,则无法直接提交到官方存储库,因此稍后将无法参与文档创作。If you clone a docs repository directly to your local system, you won't be able to make commits directly against the official repository, so you won't be able to make documentation contributions later. 如果你希望保留以后参与文档创作的机会,请使用之前介绍的分叉和克隆过程。Use the fork and clone procedure previously described if you want to preserve the opportunity to contribute to the documentation later.
  2. 在存储库的文件夹中导航到示例的位置。Navigate within the repository's folders to the sample's location. 当你点击示例链接时,示例位置的相对路径将显示在浏览器的地址栏中。The relative path to the sample's location appears in your browser's address bar when you follow the link to the sample.
  3. 要运行示例,你有多个选择:To run a sample, you have several options:
    • 使用 dotnet CLI 工具:在控制台窗口中,导航到示例的文件夹,然后使用 dotnet CLI 命令。Use the dotnet CLI tools: In a console window, navigate to the sample's folder and use dotnet CLI commands.
    • 退出 Visual StudioVisual Studio for Mac:从菜单栏中依次选择“文件”>“打开”>“项目/解决方案”来打开示例,导航到示例项目文件夹,然后选择项目文件(.csproj 或 .fsproj)。Use Visual Studio or Visual Studio for Mac: Open the sample by selecting File > Open > Project/Solution from the menu bar, navigate to the sample project folder, and select the project file (.csproj or .fsproj).
    • 使用 Visual Studio Code:从菜单栏中选择“文件”>“打开文件夹”来打开示例,然后选择示例的项目文件夹。Use Visual Studio Code: Open the sample by selecting File > Open Folder from the menu bar and selecting the sample's project folder.
    • 使用支持 .NET Core 项目的其他 IDE。Use a different IDE that supports .NET Core projects.