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. 如需詳細資訊,請參閱將 Visual Studio IDE 個人化For more information, see Personalizing the IDE.

建立新的應用程式Create a new application

啟動 Visual Studio。Start Visual Studio. 您會在 Windows 上看到下列影像:You'll see the following image on Windows:

Visual Studio Windows 上的歡迎畫面

選取影像右下角的 [建立新專案]。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)


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