教程:使用 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.

增强应用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.

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

将 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.

按任意键退出程序。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.

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

  1. 【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )

    文章目录 一.安装 C++ 桌面开发组件 二.创建并运行 Windows 控制台程序 一.安装 C++ 桌面开发组件 打开 Visual Studio Installer , 点击 " 修改 ...

  2. 使用Visual Studio Code调试.net控制台应用程序的方法

    该文章的最新版本已迁移至个人博客[比特飞],单击链接:使用Visual Studio Code调试.net控制台应用程序的方法 | .Net中文网. 1.概述 本文向大家介绍使用Visual Stud ...

  3. Linux操作系统Ubuntu 22.04配置Visual Studio Code与C++代码开发环境的方法

      本文介绍在Linux Ubuntu操作系统下,配置Visual Studio Code软件与C++ 代码开发环境的方法.   在文章虚拟机VMware Workstation Pro中配置Linu ...

  4. 【实验手册】使用Visual Studio Code 开发.NET Core应用程序

    .NET Core with Visual Studio Code 目录 概述... 2 先决条件... 2 练习1: 安装和配置.NET Core以及Visual Studio Code 扩展... ...

  5. 使用Visual Studio Code 开发.NET Core应用程序

    开源和跨平台开发是Microsoft 的当前和将来至关重要的策略..NET Core已开源,同时开发了其他项来使用和支持新的跨平台策略..NET Core2.0 目前已经发布,是适用于针对 Web 和 ...

  6. Visual studio Code User Installer(用户安装程序)和System Installer(系统安装程式)的区别

    user版会安装在当前计算机帐户目录,这意味着如果使用另一个帐号登陆计算机将无法使用别人安装的vscode. 而system版本可以安装在非用户目录,例如C盘根目录,任何帐户都可以使用.这个是主要的区 ...

  7. vscode新建html运行js,用Visual Studio Code创建JavaScript运行环境

    什么是VSCode Visual Studio Code (简称 VS Code / VSC) 是一款于2015年由微软免费开源的现代化轻量级代码编辑器,支持几乎所有主流的开发语言的语法高亮.智能代码 ...

  8. visual 创建c语言程序吗,visual studio怎么创建c语言

    visual studio创建c语言程序的方法:首先打开VS主程序,选择创建新项目:然后在菜单栏中选择C++:接着进入VS主界面,在右边的解决方案管理器中找到源文件,右键添加,并选择新建项:最后选择添 ...

  9. Visual Studio Code打开终端控制台

    刚学习Node.js开发,使用vscode开发工具.一开始使用Windows命令窗口输出Node结果,但是觉得太麻烦了,每次都要从vscode开发工具切换到Windows命令窗口,来来回回. 然后想, ...

最新文章

  1. matlab图像处理命令(二)
  2. Lucene学习笔记:Field.Store.* 域存储选项
  3. MYSQL、SQL在LIKE里传的参数没有赋进去的原因
  4. 函数 php_PHP回调函数及匿名函数概念与用法详解
  5. IBatis.net介绍
  6. centos who命令 查看当前登录系统用户信息
  7. 到底要不要拯救地球?真·逻辑鬼才!| 今日最佳
  8. Feign数据压缩传输
  9. Barra 结构化风险模型实现(1)——沪深300指数的风格因子暴露度分析
  10. raw socket java_记一次蛋疼的Raw socket发送经历。附:Raw socket编程总结
  11. oracle18c静默安装教程,Oracle 18c 通过 RPM 包安装数据库示例
  12. Linux:it is too simplistic/systematic解决办法~
  13. 利用Python爬虫爬取淘宝商品做数据挖掘分析实战篇,超详细教程!
  14. Python开发指南[1]之程序员计时小时钟(附源码)
  15. 非常好的免费开源网站原型图设计工具
  16. 解决gpu没有运行进程,但是显存一直占用的方式
  17. IP座席接入系统方案
  18. php之Twitter第三方登录
  19. 用python绘制玫瑰花的代码_Python 玫瑰花绘制
  20. 《歌剧魅影》(Phantom of the Opera)

热门文章

  1. 在web上面显示地图并定位
  2. js 身份证 港澳通行证正则
  3. FSCapture 录制视频没有声音的解决方法
  4. Guava前置条件Preconditions类(参数校验)
  5. Cisco-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
  6. 如何设置计算机自动连接宽带,宽带自动连接设置,教您电脑怎么设置宽带自动连接...
  7. 计算机组成原理选择题
  8. 快速上手云原生安全平台 NeuVector
  9. warning: #231-D: declaration is not visible outside of function
  10. 穷人如何使用测试驱动开发进行重构