Hello World -- 你的第一个程序Hello World -- Your first program

在本文中,你将使用 Visual Studio 创建传统的“Hello World!”In this article, you'll use Visual Studio to create the traditional "Hello World!" 版本。program. Visual Studio 是一个专业的集成开发环境 (IDE),具有适用于 .NET 开发的许多功能。Visual Studio is a professional Integrated Development Environment (IDE) with many features designed for .NET development. 只需使用 Visual Studio 中的几个功能即可创建此程序。You'll use only a few of the features in Visual Studio to create this program. 如需了解有关 Visual Studio 的详细信息,请参阅 Visual C# 入门To learn more about Visual Studio, see Getting Started with Visual C#.

备注

以下说明中的某些 Visual Studio 用户界面元素在计算机上出现的名称或位置可能会不同。Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. 这些元素取决于你所使用的 Visual Studio 版本和你所使用的设置。The Visual Studio edition that you have and the settings that you use determine these elements. 有关详细信息,请参阅个性化设置 IDEFor more information, see Personalizing the IDE.

创建一个新的应用程序Create a new application

启动 Visual Studio。Start Visual Studio. 你将在 Windows 上看到以下图像:You'll see the following image on Windows:

Windows 上的 Visual Studio 欢迎屏幕

在图像右下角选择“创建新项目”。Select Create a new project in the lower right corner of the image. Visual Studio 会显示“新建项目”对话框:Visual Studio displays the New Project dialog:

Windows 上的 Visual Studio 新建项目屏幕

备注

如果这是首次启动 Visual Studio,则“最近使用的项目模板”列表为空。If this is the first time you've started Visual Studio, the Recent project templates list is empty.

在“新建项目”对话框中,选择“控制台应用(.NET Core)”,然后按“下一步”。On the new project dialog, choose "Console App (.NET Core)" and then press Next. 请为项目命名(例如“HelloWorld”),然后按“创建”。Give your project a name, such as "HelloWorld", then press Create.

Visual Studio 将打开你的项目。Visual Studio opens your project. 一个基本的“Hello World!”已初见雏形It's already a basic "Hello World!" 示例。example. Ctrl + F5 运行项目。Press Ctrl + F5 to run your project. Visual Studio 生成项目,将源代码转换为可执行文件。Visual Studio builds your project, converting the source code into an executable. 然后,它会启动一个运行新应用程序的命令窗口。Then, it launches a command window that runs your new application. 你应在窗口中看到以下文本:You should see the following text in the window:

Hello World!

C:\Program Files\dotnet\dotnet.exe (process 11964) exited with code 0.
Press any key to close this window . . .

按相关键关闭窗口。Press a key to close the window.

C# 程序的元素Elements of a C# program

我们来检查一下此程序的重要部分。Let's examine the important parts of this program. 第一行包含一条注释。The first line contains a comment. 字符 // 将该行的其余内容转换为一条注释。The characters // convert the rest of the line to a comment.

// A Hello World! program in C#.

还可以通过将文本块置于 /**/ 字符之间,禁止对其的注释。You can also comment out a block of text by enclosing it between the /* and */ characters. 这在下面的示例中显示。This is shown in the following example.

/* A "Hello World!" program in C#.
This program displays the string "Hello World!" on the screen. */

C# 控制台应用程序必须包含 Main 方法,控件在其中开始和结束。A C# console application must contain a Main method, in which control starts and ends. Main 方法中,可以创建对象并执行其他方法。The Main method is where you create objects and execute other methods.

Main 方法是驻留在类或结构中的静态方法。The Main method is a static method that resides inside a class or a struct. 在上一个“Hello World!”In the previous "Hello World!" 示例中,该方法驻留在名为 Hello 的类中。example, it resides in a class named Hello. 可以通过以下方式之一来声明 Main 方法:You can declare the Main method in one of the following ways:

  • 它可返回 voidIt can return void. 这表示程序不会返回值。That means your program doesn't return a value.
static void Main()
{
    //...
}
  • 还可以返回一个整数。It can also return an integer. 整数是应用程序的“退出代码”。The integer is the exit code for your application.
static int Main()
{
    //...
    return 0;
}
  • 无论使用哪种返回类型,它都可以带有参数。With either of the return types, it can take arguments.
static void Main(string[] args)
{
    //...
}

- 或 --or-

static int Main(string[] args)
{
    //...
    return 0;
}

Main 方法中定义的参数 args 是一个 string 类型的数组,该数组包含用于调用该程序的命令行自变量。The parameter of the Main method, args, is a string array that contains the command-line arguments used to invoke the program.

若要了解更多有关如何使用命令行参数的信息,请参阅 Main() 和命令行参数中的示例。For more information about how to use command-line arguments, see the examples in Main() and Command-Line Arguments.

输入和输出Input and output

C# 程序通常使用由 .NET 的运行时库提供的输入/输出服务。C# programs generally use the input/output services provided by the run-time library of .NET. 语句 System.Console.WriteLine("Hello World!"); 使用 WriteLine 方法。The statement System.Console.WriteLine("Hello World!"); uses the WriteLine method. 这是运行时库中 Console 类的输出方法之一。This is one of the output methods of the Console class in the run-time library. 该方法将在标准输出流中显示其字符串参数,后接新行。It displays its string parameter on the standard output stream followed by a new line. 其他 Console 方法可用于不同的输入和输出操作。Other Console methods are available for different input and output operations. 如果程序开头包含 using System; 指令,则可以直接使用 System 类和方法,而不必进行完全限定。If you include the using System; directive at the beginning of the program, you can directly use the System classes and methods without fully qualifying them. 例如,可以调用 Console.WriteLine,而非 System.Console.WriteLineFor example, you can call Console.WriteLine instead of System.Console.WriteLine:

using System;
Console.WriteLine("Hello World!");

有关输入/输出方法的详细信息,请参阅 System.IOFor more information about input/output methods, see System.IO.

请参阅See also