教程:使用 Visual Studio Code 创建 .NET Core 控制台应用程序Tutorial: Create a .NET Core console application using Visual Studio Code

本教程演示如何使用 Visual Studio Code 和 .NET Core CLI 创建并运行 .NET Core 控制台应用程序。This tutorial shows how to create and run a .NET Core console application by using Visual Studio Code and the .NET Core CLI. 项目任务(例如创建、编译和运行项目)通过使用 .NET Core CLI 来完成。Project tasks, such as creating, compiling, and running a project are done by using the .NET Core CLI. 你可以遵循本教程中的步骤使用其他代码编辑器,然后在终端中运行命令(如果你愿意)。You can follow this tutorial with a different code editor and run commands in a terminal if you prefer.

先决条件Prerequisites

  1. 已安装 C# 扩展Visual Studio CodeVisual Studio Code with the C# extension installed. 有关如何在 Visual Studio Code 上安装扩展的信息,请访问 VS Code 扩展市场For information about how to install extensions on Visual Studio Code, see VS Code Extension Marketplace.
  2. .NET Core 3.1 SDK 或更高版本The .NET Core 3.1 SDK or later

创建应用Create the app

创建一个名为“HelloWorld”的 .NET Core 控制台应用项目。Create a .NET Core console app project named "HelloWorld".

  1. 启动 Visual Studio Code。Start Visual Studio Code.

  2. 从主菜单中选择“文件” > “打开文件夹”(在 macOS 上为“文件” > “打开...”)。Select File > Open Folder (File > Open... on macOS) from the main menu.

  3. 在“打开文件夹”对话框中,创建“HelloWorld”文件夹,然后单击“选择文件夹”(在 macOS 上为“打开”)。In the Open Folder dialog, create a HelloWorld folder and click Select Folder (Open on macOS).

    默认情况下,文件夹名称将是项目名称和命名空间名称。The folder name becomes the project name and the namespace name by default. 稍后将在本教程中添加代码,假定项目命名空间为 HelloWorldYou'll add code later in the tutorial that assumes the project namespace is HelloWorld.

  4. 在主菜单中选择“视图” > “终端”,从 Visual Studio Code 中打开“终端” 。Open the Terminal in Visual Studio Code by selecting View > Terminal from the main menu.

    “终端”在“HelloWorld”文件夹中连同命令提示符一起打开。The Terminal opens with the command prompt in the HelloWorld folder.

  5. 在“终端”中输入以下命令:In the Terminal, enter the following command:

    dotnet new console
    

用于创建简单的“Hello World”应用程序的模板。The template creates a simple "Hello World" application. 调用 Console.WriteLine(String) 方法以在控制台窗口中显示“Hello World!”。It calls the Console.WriteLine(String) method to display "Hello World!" in the console window.

模板代码将定义类 Program,其中包含一个需要将 String 数组用作参数的方法 MainThe template code defines a class, Program, with a single method, Main, that takes a String array as an argument:

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Main 是应用程序入口点,同时也是在应用程序启动时由运行时自动调用的方法。Main is the application entry point, the method that's called automatically by the runtime when it launches the application. args 数组中包含在应用程序启动时提供的所有命令行自变量。Any command-line arguments supplied when the application is launched are available in the args array.

运行应用Run the app

在“终端”中运行以下命令:Run the following command in the Terminal:

dotnet run

程序显示“Hello World!”The program displays "Hello World!" 然后结束。and ends.

dotnet run 命令

增强应用Enhance the app

改进应用程序,使其提示用户输入名字,并将其与日期和时间一同显示。Enhance the application to prompt the user for their name and display it along with the date and time.

  1. 单击打开 Program.csOpen Program.cs by clicking on it.

    在 Visual Studio Code 中首次打开 C# 文件时,会在编辑器中加载 OmniSharpThe first time you open a C# file in Visual Studio Code, OmniSharp loads in the editor.

    打开 Program.cs 文件

  2. Visual Studio Code 提示添加缺少的资产时选择“是”,以生成和调试应用。Select Yes when Visual Studio Code prompts you to add the missing assets to build and debug your app.

    提示添加缺少的资产

  3. 将 Program.cs 中 Main 方法的内容(当前只是调用 Console.WriteLine 的行)替换为以下代码:Replace the contents of the Main method in Program.cs, which is the line that calls Console.WriteLine, with the following code:

    Console.WriteLine("\nWhat is your name? ");
    var name = Console.ReadLine();
    var date = DateTime.Now;
    Console.WriteLine($"\nHello, {name}, on {date:d} at {date:t}!");
    Console.Write("\nPress any key to exit...");
    Console.ReadKey(true);
    

    此代码会在控制台窗口中显示一条提示,然后等待用户输入字符串并按 EnterThis code displays a prompt in the console window and waits until the user enters a string followed by the Enter key. 它会将此字符串存储到名为 name 的变量中。It stores this string in a variable named name. 它还会检索 DateTime.Now 属性的值(其中包含当前的本地时间),并将此值赋给 date 变量。It also retrieves the value of the DateTime.Now property, which contains the current local time, and assigns it to a variable named date. 同时会在控制台窗口中显示这些值。And it displays these values in the console window. 最后会在控制台窗口中显示一条提示,并调用 Console.ReadKey(Boolean) 方法来等待用户输入。Finally, it displays a prompt in the console window and calls the Console.ReadKey(Boolean) method to wait for user input.

    \n 表示一个换行符。The \n represents a newline character.

    字符串前面的美元符号 ($) 使你可以将表达式(如变量名称)放入字符串中的大括号内。The dollar sign ($) in front of a string lets you put expressions such as variable names in curly braces in the string. 表达式值将代替表达式插入到字符串中。The expression value is inserted into the string in place of the expression. 此语法称为内插字符串This syntax is referred to as interpolated strings.

  4. 保存更改。Save your changes.

    重要

    在 Visual Studio Code 中,必须显式保存更改。In Visual Studio Code, you have to explicitly save changes. 与 Visual Studio 不同,生成和运行应用时不会自动保存文件更改。Unlike Visual Studio, file changes are not automatically saved when you build and run an app.

  5. 再次运行程序:Run the program again:

    dotnet run
    
  6. 出现提示时,输入名称并按 Enter 键。Respond to the prompt by entering a name and pressing the Enter key.

    包含经过修改的程序输出的“终端”窗口

  7. 按任意键退出程序。Press any key to exit the program.

其他资源Additional resources

后续步骤Next steps

在本教程中,你创建了一个 .NET Core 控制台应用程序。In this tutorial, you created a .NET Core console application. 在下一教程中,你将调试该应用。In the next tutorial, you debug the app.