linux .net 控制台应用程序,使用 Visual Studio Code 创建 .NET 控制台应用程序 - .NET | Microsoft Docs...

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

11/17/2020

本文内容

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

先决条件Prerequisites

有关如何在 Visual Studio Code 上安装扩展的信息,请访问 VS Code 扩展市场。For information about how to install extensions on Visual Studio Code, see VS Code Extension Marketplace.

创建应用Create the app

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

启动 Visual Studio Code。Start Visual Studio Code.

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

在“打开文件夹”对话框中,创建“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. 稍后将在本教程中添加代码,假定项目命名空间为 HelloWorld。You'll add code later in the tutorial that assumes the project namespace is HelloWorld.

在主菜单中选择“视图” > “终端”,从 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.

在“终端”中输入以下命令: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 数组用作参数的方法 Main:The 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.

e0c3de1963d3708991966ad71c1e28d9.png

增强应用Enhance the app

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

单击打开 Program.cs。Open Program.cs by clicking on it.

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

a1a825bc3a6e9e4862e64404094b462c.png

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

deaaeb07fe72d1b890e2c37948c87e67.png

将 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("What is your name?");

var name = Console.ReadLine();

var currentDate = DateTime.Now;

Console.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}!");

Console.Write($"{Environment.NewLine}Press any key to exit...");

Console.ReadKey(true);

此代码会在控制台窗口中显示一条提示,然后等待用户输入字符串并按 Enter。This 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.

NewLine 是一种独立于平台和语言的表示换行符的方式。NewLine is a platform-independent and language-independent way to represent a line break. 替代方法是在 C# 中使用 \n 和在 Visual Basic 中使用 vbCrLf。Alternatives are \n in C# and vbCrLf in Visual Basic.

字符串前面的美元符号 ($) 使你可以将表达式(如变量名称)放入字符串中的大括号内。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.

保存更改。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.

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

dotnet run

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

c35972dc1127e4e2f4f2778980e138e8.png

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

其他资源Additional resources

后续步骤Next steps

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值