歡迎使用 Visual Studio IDEWelcome to the Visual Studio IDE

Visual Studio「整合式開發環境」是一個有創意的啟動控制板,可供您編輯、偵錯及建置程式碼,然後發佈應用程式。The Visual Studio integrated development environment is a creative launching pad that you can use to edit, debug, and build code, and then publish an app. 整合式開發環境 (IDE) 是功能豐富的程式,可用於軟體開發的許多方面。An integrated development environment (IDE) is a feature-rich program that can be used for many aspects of software development. 除了大部分 IDE 提供的標準編輯器和偵錯工具之外,Visual Studio 還有編譯器、程式碼完成工具、圖形設計工具和更多功能,讓軟體開發程序變得更為容易。Over and above the standard editor and debugger that most IDEs provide, Visual Studio includes compilers, code completion tools, graphical designers, and many more features to ease the software development process.

Visual Studio 2017 IDE

Visual Studio 2019 IDEThe Visual Studio 2019 IDE

此圖顯示 Visual Studio,其中包含一個開啟的專案,以及您想要使用的數個重要工具視窗:This image shows Visual Studio with an open project and several key tool windows you'll likely use:

  • 「方案總管」(右上) 可讓您檢視、巡覽及管理您的程式碼檔案。Solution Explorer (top right) lets you view, navigate, and manage your code files. 方案 Explorer 可將檔案分組到 方案和專案中,以協助組織程式碼。Solution Explorer can help organize your code by grouping the files into solutions and projects.

  • 「編輯器視窗」(中間) 會顯示檔案內容,您大部分的時間可能都是花在這裡。The editor window (center), where you'll likely spend a majority of your time, displays file contents. 您在這裡編輯程式碼或設計使用者介面,例如有按鈕和文字方塊的視窗。This is where you can edit code or design a user interface such as a window with buttons and text boxes.

  • 輸出視窗 (中下) 是 Visual Studio 傳送通知的位置,例如偵錯和錯誤訊息、編譯器警告、發佈狀態訊息等。The Output window (bottom center) is where Visual Studio sends notifications such as debugging and error messages, compiler warnings, publishing status messages, and more. 每個訊息來源都有自己的索引標籤。Each message source has its own tab.

版本Editions

Visual Studio 適用於 Windows 和 Mac。Visual Studio is available for Windows and Mac. Visual Studio for Mac 有許多與 Visual Studio 2017 相同的功能,並已針對開發跨平台和行動應用程式最佳化。Visual Studio for Mac has many of the same features as Visual Studio 2017, and is optimized for developing cross-platform and mobile apps. 本文著重於 Windows 版的 Visual Studio 2017。This article focuses on the Windows version of Visual Studio 2017.

Visual Studio 共有三種版本:「社區」、「專業」和「企業」。There are three editions of Visual Studio: Community, Professional, and Enterprise. 請參閱 比較 Visual Studio 版本 ,以瞭解每個版本支援哪些功能。See Compare Visual Studio editions to learn about which features are supported in each edition.

Visual Studio 適用於 Windows 和 Mac。Visual Studio is available for Windows and Mac. Visual Studio for Mac 有許多與 Visual Studio 2019 相同的功能,並已針對開發跨平台和行動應用程式最佳化。Visual Studio for Mac has many of the same features as Visual Studio 2019, and is optimized for developing cross-platform and mobile apps. 此文章著重於 Windows 版的 Visual Studio 2019。This article focuses on the Windows version of Visual Studio 2019.

有三種版本的 Visual Studio 2019:「社團」、「專業」和「企業」。There are three editions of Visual Studio 2019: Community, Professional, and Enterprise. 請參閱 比較 Visual Studio 版本 ,以瞭解每個版本支援哪些功能。See Compare Visual Studio editions to learn about which features are supported in each edition.

Visual Studio 的某些熱門功能可在您開發軟體時協助您提高生產力,這些功能包括:Some of the popular features in Visual Studio that help you to be more productive as you develop software include:

  • 波浪線和快速動作Squiggles and Quick Actions

    波浪線是波浪底線,可在您鍵入程式碼時,針對錯誤或潛在問題提出警示。Squiggles are wavy underlines that alert you to errors or potential problems in your code as you type. 這些視覺提示可讓您立即修正問題,不必等到建置期間或執行程式時發現錯誤。These visual clues enable you to fix problems immediately without waiting for the error to be discovered during build or when you run the program. 如果您將滑鼠停留在波浪線,您會看到有關此錯誤的其他資訊。If you hover over a squiggle, you see additional information about the error. 左邊界也可能會出現燈泡與修正錯誤的動作,稱為「快速動作」。A light bulb may also appear in the left margin with actions, known as Quick Actions, to fix the error.

    Visual Studio 的波浪線

  • 程式碼清除Code Cleanup

    透過按一下按鈕,可將您的程式碼格式化,並套用您的程式碼樣式設定.editorconfig 慣例Roslyn 分析器所建議的任何程式碼修正。With the click of a button, format your code and apply any code fixes suggested by your code style settings, .editorconfig conventions, and Roslyn analyzers. 程式碼清除 可協助您在程式碼進入程式碼檢閱之前先解決其中的問題Code Cleanup helps you resolve issues in your code before it goes to code review. (目前僅適用於 C# 程式碼)。(Currently available for C# code only.)

    Visual Studio 中的 [程式碼清除] 按鈕

  • 重構Refactoring

    重構作業包括:智慧型重新命名變數、擷取一或多行程式碼放入新方法、變更方法參數順序及更多。Refactoring includes operations such as intelligent renaming of variables, extracting one or more lines of code into a new method, changing the order of method parameters, and more.

    在 Visual Studio 中重構

  • 智慧IntelliSense

    IntelliSense 為一組功能的字詞,會直接在編輯器中顯示有關您程式碼的資訊,而在某些情況下會為您撰寫一些程式碼。IntelliSense is a term for a set of features that displays information about your code directly in the editor and, in some cases, write small bits of code for you. 就像內嵌在編輯器中的基本文件,讓您不必在其他位置查閱類型資訊。It's like having basic documentation inline in the editor, which saves you from having to look up type information elsewhere. IntelliSense 功能會因語言而異。IntelliSense features vary by language. 如需詳細資訊,請參閱 c # intellisenseVisual c + + IntelliSenseJavaScript intellisensevisual Basic intellisenseFor more information, see C# IntelliSense, Visual C++ IntelliSense, JavaScript IntelliSense, and Visual Basic IntelliSense. 下圖顯示 IntelliSense 如何顯示類型的成員清單:The following illustration shows how IntelliSense displays a member list for a type:

    Visual Studio 成員清單

  • Visual Studio 搜尋Visual Studio search

    Visual Studio 使用這麼多的功能表、選項和屬性,有時似乎讓人有壓迫感。Visual Studio can seem overwhelming at times with so many menus, options, and properties. Visual Studio 搜尋 (Ctrl + Q) 是在一個位置快速尋找 IDE 功能和程式碼的絕佳方法。Visual Studio search (Ctrl+Q) is a great way to rapidly find IDE features and code in one place.

    Visual Studio 2017 中的 [快速啟動] 搜尋方塊

    如需詳細資訊,請參閱快速啟動For more information, see Quick Launch.

    Visual Studio 2019 中的搜尋方塊

    如需資訊和生產力秘訣,請參閱 如何使用 Visual Studio 搜尋For information and productivity tips, see How to use Visual Studio search.

  • Live ShareLive Share

    即時與其他人共同編輯和偵錯,而無論您的應用程式類型或程式設計語言為何。Collaboratively edit and debug with others in real time, regardless of what your app type or programming language. 您可以立即且安全地共用專案,然後視需要偵錯工作階段、終端機執行個體、localhost Web 應用程式、語音通話等。You can instantly and securely share your project and, as needed, debugging sessions, terminal instances, localhost web apps, voice calls, and more.

  • 呼叫階層Call Hierarchy

    [呼叫階層] 視窗會顯示呼叫所選方法的方法。The Call Hierarchy window shows the methods that call a selected method. 當您考慮要變更或移除方法,或嘗試追蹤 Bug 時,這會是有用的資訊。This can be useful information when you're thinking about changing or removing the method, or when you're trying to track down a bug.

    呼叫階層視窗

  • CodeLensCodeLens

    CodeLens 可協助您尋找程式碼的參考、程式碼的變更、已連結的 Bug、工作項目、程式碼檢閱和單元測試,全都不用離開編輯器。CodeLens helps you find references to your code, changes to your code, linked bugs, work items, code reviews, and unit tests, all without leaving the editor.

    CodeLens

  • 移至定義Go To Definition

    [移至定義] 功能就可以直接帶您進入定義函式或類型的位置。The Go To Definition feature takes you directly to the location where a function or type is defined.

    移至定義

  • 查看定義Peek Definition

    [查看定義] 視窗會顯示方法或類型的定義,不必實際開啟個別的檔案。The Peek Definition window shows the definition of a method or type without actually opening a separate file.

    查看定義

安裝 Visual Studio IDEInstall the Visual Studio IDE

在本節中,您將建立一個簡單的專案,以試用一些您可透過 Visual Studio 執行的動作。In this section, you'll create a simple project to try out some of the things you can do with Visual Studio. 您將使用 IntelliSense 作為編碼協助工具、在程式執行期間偵錯應用程式以查看變數值,以及變更色彩佈景主題。You'll use IntelliSense as a coding aid, debug an app to see the value of a variable during the program's execution, and change the color theme.

若要開始使用,請 下載 Visual Studio ,並將它安裝在您的系統上。To get started, download Visual Studio and install it on your system. 模組安裝程式可讓您選擇並安裝「工作負載」,這些通常是您慣用的程式設計語言或平台所需的幾組功能。The modular installer enables you to choose and install workloads, which are groups of features needed for the programming language or platform you prefer. 若要遵循 建立程式的步驟,請務必在安裝期間選取 .NET Core 跨平台開發 工作負載。To follow the steps for creating a program, be sure to select the .NET Core cross-platform development workload during installation.

若要開始使用,請 下載 Visual Studio ,並將它安裝在您的系統上。To get started, download Visual Studio and install it on your system. 模組安裝程式可讓您選擇並安裝「工作負載」,這些通常是您慣用的程式設計語言或平台所需的幾組功能。The modular installer enables you to choose and install workloads, which are groups of features needed for the programming language or platform you prefer. 若要遵循 建立程式的步驟,請務必在安裝期間選取 .NET Core 跨平台開發 工作負載。To follow the steps for creating a program, be sure to select the .NET Core cross-platform development workload during installation.

Visual Studio 安裝程式中的 .NET Core 跨平台開發工作負載

當您第一次開啟 Visual Studio 時,可以選擇性地使用您的 Microsoft 帳戶或是公司或學校帳戶來登入When you open Visual Studio for the first time, you can optionally sign in using your Microsoft account or your work or school account.

建立程式Create a program

讓我們來深入探討並建立一個簡單的程式。Let's dive in and create a simple program.

  1. 開啟 Visual Studio。Open Visual Studio.

  2. 在功能表列上 ,選擇 [ 檔案 > 新增 > 專案]。On the menu bar, choose File > New > Project.

    功能表列上的 [檔案] > [新增專案]

    [ 新增專案 ] 對話方塊會顯示數個專案 範本The New Project dialog box shows several project templates. 範本包含指定專案類型所需的基本檔案和設定。A template contains the basic files and settings needed for a given project type.

  3. 選擇 [Visual C#] 下的 [.NET Core] 範本類別,然後選擇 [主控台應用程式 (.NET Core)] 範本。Choose the .NET Core template category under Visual C#, and then choose the Console App (.NET Core) template. 在 [名稱] 文字方塊中,鍵入 HelloWorld,然後選取 [確定] 按鈕。In the Name text box, type HelloWorld, and then select the OK button.

    .NET Core 應用程式範本

    注意

    如果您未看到 [.NET Core] 類別,則需要安裝 [.NET Core 跨平台開發] 工作負載。If you don't see the .NET Core category, you need to install the .NET Core cross-platform development workload. 若要安裝,請選擇 [新增專案] 對話方塊左下角的 [開啟 Visual Studio 安裝程式] 連結。To do this, choose the Open Visual Studio Installer link on the bottom left of the New Project dialog. 在 Visual Studio 安裝程式開啟後,向下捲動並選取 [.NET Core 跨平台開發] 工作負載,然後選取 [修改]。After Visual Studio Installer opens, scroll down and select the .NET Core cross-platform development workload, and then select Modify.

    Visual Studio 會建立專案。Visual Studio creates the project. 其為簡單的 "Hello World" 應用程式,會呼叫 Console.WriteLine() 方法來顯示常值字串 "Hello World!"It's a simple "Hello World" application that calls the Console.WriteLine() method to display the literal string "Hello World!" 在主控台 (程式輸出) 視窗中。in the console (program output) window.

    您應該會立即看到類似下列的畫面:Shortly, you should see something like the following:

    Visual Studio IDE

    您應用程式的 C# 程式碼會顯示在編輯器視窗中,占據大部分的空間。The C# code for your application shows in the editor window, which takes up most of the space. 請注意,系統會將文字自動標示色彩,以表示不同部分的程式碼,例如關鍵字和類型。Notice that the text is automatically colorized to indicate different parts of the code, such as keywords and types. 此外,程式碼中的垂直小虛線會指出那些大括號彼此成對,而行號則可協助您稍後找出程式碼。In addition, small, vertical dashed lines in the code indicate which braces match one another, and line numbers help you locate code later. 您可以選擇帶方框的小負號來摺疊或展開程式碼區塊。You can choose the small, boxed minus signs to collapse or expand blocks of code. 此程式碼大綱功能可讓您隱藏您不需要的程式碼,有助於讓畫面變得較為簡潔。This code outlining feature lets you hide code you don't need, helping to minimize onscreen clutter. 專案檔會列在右邊稱作 [方案總管] 的視窗中。The project files are listed on the right side in a window called Solution Explorer.

    具有紅色方塊的 Visual Studio IDE

    還有其他可用的功能表和工具視窗,但讓我們目前先繼續進行操作。There are other menus and tool windows available, but let's move on for now.

  4. 現在,啟動應用程式。Now, start the app. 您可以藉由從功能表列上的 [偵錯] 功能表選擇 [啟動但不偵錯],來執行此動作。You can do this by choosing Start Without Debugging from the Debug menu on the menu bar. 您也可以按下 Ctrl + F5You can also press Ctrl+F5.

    [偵錯] > [啟動但不偵錯] 功能表

    Visual Studio 會建立應用程式,而主控台視窗則會開啟,並顯示訊息 Hello World!Visual Studio builds the app, and a console window opens with the message Hello World!. 您現在已有一個執行中的應用程式!You now have a running app!

    顯示輸出 ' Hello Word! ' 的 cmd.exe 主控台視窗的螢幕擷取畫面

  5. 若要關閉主控台視窗,請在鍵盤上按下任意鍵。To close the console window, press any key on your keyboard.

  6. 讓我們將一些其他程式碼新增至應用程式。Let's add some additional code to the app. Console.WriteLine("Hello World!"); 行前新增下列 C# 程式碼:Add the following C# code before the line that says Console.WriteLine("Hello World!");:

    Console.WriteLine("\nWhat is your name?");
    var name = Console.ReadLine();
    

    此程式碼會在主控台視窗中顯示 What is your name?,然後等待使用者輸入某些文字並按下 Enter 鍵。This code displays What is your name? in the console window, and then waits until the user enters some text followed by the Enter key.

  7. Console.WriteLine("Hello World!"); 行變更為下列程式碼:Change the line that says Console.WriteLine("Hello World!"); to the following code:

    Console.WriteLine($"\nHello {name}!");
    
  8. 選取 [偵錯][啟動但不偵錯] > 或按 Ctrl+F5,再次執行應用程式。Run the app again by selecting Debug > Start Without Debugging or by pressing Ctrl+F5.

    Visual Studio 會重建應用程式,然後主控台視窗會開啟並提示您輸入您的名稱。Visual Studio rebuilds the app, and a console window opens and prompts you for your name.

  9. 請在主控台視窗中輸入您的名稱,並按下 ENTEREnter your name in the console window and press Enter.

    主控台視窗輸入

  10. 按任意鍵以關閉主控台視窗並停止執行程式。Press any key to close the console window and stop the running program.

  1. 開啟 Visual Studio。Open Visual Studio.

    [開始] 視窗即會出現,並顯示可用來複製存放庫、開啟最近專案或建立全新專案的各種選項。The start window appears with various options for cloning a repo, opening a recent project, or creating a brand new project.

  2. 選擇 [ 建立新專案]。Choose Create a new project.

    Visual Studio 2019 中 [建立新專案] 視窗的螢幕擷取畫面。

    [建立新專案] 視窗隨即開啟,並顯示數個專案「範本」。The Create a new project window opens and shows several project templates. 範本包含指定專案類型所需的基本檔案和設定。A template contains the basic files and settings required for a given project type.

  3. 若要尋找所需的範本,在搜尋方塊中鍵入或輸入 .net core 主控台To find the template we want, type or enter .net core console in the search box. 系統即會根據您所輸入的關鍵字自動篩選可用的範本清單。The list of available templates is automatically filtered based on the keywords you entered. 您可以從 [所有語言] 下拉式清單中選擇 [ c # ]、[所有平臺] 清單中的 [ Windows ] 和 [所有專案類型] 清單中的 [主控台],進一步篩選範本結果。You can further filter the template results by choosing C# from the All language drop-down list, Windows from the All platforms list, and Console from the All project types list .

    選取 [ 主控台應用程式 ] 範本,然後按 [下一步]Select the Console Application template, and then click Next.

    Visual Studio 2019 中 [建立新專案] 視窗的螢幕擷取畫面,您可以在其中選取所需的範本。

  4. 在 [設定您的新專案] 視窗中,于 [專案名稱] 方塊中輸入 HelloWorld ,選擇性地變更專案檔的目錄位置 (預設的地區設定為 C:\Users\<name>\source\repos) ,然後按 [下一步]In the Configure your new project window, enter HelloWorld in the Project name box, optionally change the directory location for your project files (the default locale is C:\Users\<name>\source\repos), and then click Next.

    Visual Studio 2019 中 [設定您的新專案] 視窗的螢幕擷取畫面,您可以在其中輸入專案的名稱。

  5. 在 [其他資訊] 視窗中,確認 [目標 Framework ] 下拉式功能表中出現 .net Core 3.1 ,然後按一下 [建立]。In the Additional information window, verify that .NET Core 3.1 appears in the Target Framework drop-down menu, and then click Create.

    Visual Studio 2019 中 [其他資訊] 視窗的螢幕擷取畫面,您可以在其中選取所需的 .NET Core Framework 版本。

    Visual Studio 會建立專案。Visual Studio creates the project. 其為簡單的 "Hello World" 應用程式,會呼叫 Console.WriteLine() 方法來顯示常值字串 "Hello World!"It's a simple "Hello World" application that calls the Console.WriteLine() method to display the literal string "Hello World!" 在主控台 (程式輸出) 視窗中。in the console (program output) window.

    您應該會立即看到類似下列的畫面:Shortly, you should see something like the following:

    Visual Studio IDE

    您應用程式的 C# 程式碼會顯示在編輯器視窗中,占據大部分的空間。The C# code for your application shows in the editor window, which takes up most of the space. 請注意,系統會將文字自動標示色彩,以表示不同部分的程式碼,例如關鍵字和類型。Notice that the text is automatically colorized to indicate different parts of the code, such as keywords and types. 此外,程式碼中的垂直小虛線會指出那些大括號彼此成對,而行號則可協助您稍後找出程式碼。In addition, small, vertical dashed lines in the code indicate which braces match one another, and line numbers help you locate code later. 您可以選擇帶方框的小負號來摺疊或展開程式碼區塊。You can choose the small, boxed minus signs to collapse or expand blocks of code. 此程式碼大綱功能可讓您隱藏您不需要的程式碼,有助於讓畫面變得較為簡潔。This code outlining feature lets you hide code you don't need, helping to minimize onscreen clutter. 專案檔會列在右邊稱作 [方案總管] 的視窗中。The project files are listed on the right side in a window called Solution Explorer.

    具有紅色方塊的 Visual Studio IDE

    還有其他可用的功能表和工具視窗,但讓我們目前先繼續進行操作。There are other menus and tool windows available, but let's move on for now.

  6. 現在,啟動應用程式。Now, start the app. 您可以藉由從功能表列上的 [偵錯] 功能表選擇 [啟動但不偵錯],來執行此動作。You can do this by choosing Start Without Debugging from the Debug menu on the menu bar. 您也可以按下 Ctrl + F5You can also press Ctrl+F5.

    [偵錯] > [啟動但不偵錯] 功能表

    Visual Studio 會建立應用程式,而主控台視窗則會開啟,並顯示訊息 Hello World!Visual Studio builds the app, and a console window opens with the message Hello World!. 您現在已有一個執行中的應用程式!You now have a running app!

    Microsoft Visual Studio 偵錯工具主控台視窗的螢幕擷取畫面,其中顯示輸出 ' Hello Word! '

  7. 若要關閉主控台視窗,請在鍵盤上按下任意鍵。To close the console window, press any key on your keyboard.

  8. 讓我們將一些其他程式碼新增至應用程式。Let's add some additional code to the app. Console.WriteLine("Hello World!"); 行前新增下列 C# 程式碼:Add the following C# code before the line that says Console.WriteLine("Hello World!");:

    Console.WriteLine("\nWhat is your name?");
    var name = Console.ReadLine();
    

    此程式碼會在主控台視窗中顯示 What is your name?,然後等待使用者輸入某些文字並按下 Enter 鍵。This code displays What is your name? in the console window, and then waits until the user enters some text followed by the Enter key.

  9. Console.WriteLine("Hello World!"); 行變更為下列程式碼:Change the line that says Console.WriteLine("Hello World!"); to the following code:

    Console.WriteLine($"\nHello {name}!");
    
  10. 選取 [偵錯][啟動但不偵錯] > 或按 Ctrl+F5,再次執行應用程式。Run the app again by selecting Debug > Start Without Debugging or by pressing Ctrl+F5.

    Visual Studio 會重建應用程式,然後主控台視窗會開啟並提示您輸入您的名稱。Visual Studio rebuilds the app, and a console window opens and prompts you for your name.

  11. 請在主控台視窗中輸入您的名稱,並按下 ENTEREnter your name in the console window and press Enter.

    Microsoft Visual Studio 偵錯工具主控台視窗的螢幕擷取畫面,其中顯示名稱、輸入和輸出 ' Hello Georgette! ' 的提示。

  12. 按任意鍵以關閉主控台視窗並停止執行程式。Press any key to close the console window and stop the running program.

使用重構和 IntelliSenseUse refactoring and IntelliSense

讓我們看看重構IntelliSense 這兩種方式如何協助您更有效率地撰寫程式碼。Let's look at a couple of the ways that refactoring and IntelliSense can help you code more efficiently.

首先,請重新命名 name 變數:First, let's rename the name variable:

  1. 按兩下 name 變數來選取它。Double-click the name variable to select it.

  2. 鍵入變數的新名稱 usernameType in the new name for the variable, username.

    請注意,變數周圍會出現一個灰色方塊,而邊界會出現一個燈泡。Notice that a gray box appears around the variable, and a light bulb appears in the margin.

  1. 選取燈泡圖示以顯示可用的快速動作Select the light bulb icon to show the available Quick Actions. 選取 [將 'name' 重新命名為 'username']。Select Rename 'name' to 'username'.

    重新命名 Visual Studio 中的動作

    此變數會在專案中重新命名,在我們的案例中只有兩個位置。The variable is renamed across the project, which in our case is only two places.

    顯示 Visual Studio 中重新命名重構的動畫 GIF

  1. 選取燈泡圖示以顯示可用的快速動作Select the light bulb icon to show the available Quick Actions. 選取 [將 'name' 重新命名為 'username']。Select Rename 'name' to 'username'.

    重新命名 Visual Studio 中的動作

    此變數會在專案中重新命名,在我們的案例中只有兩個位置。The variable is renamed across the project, which in our case is only two places.

  1. 現在,讓我們看看 IntelliSense。Now let's take a look at IntelliSense. Console.WriteLine($"\nHello {username}!"); 行下方,輸入 DateTime now = DateTime.Below the line that says Console.WriteLine($"\nHello {username}!");, type DateTime now = DateTime..

    一個方塊會顯示 DateTime 類別的成員。A box displays the members of the DateTime class. 此外,目前選取成員的描述會顯示在另一個方塊中。In addition, the description of the currently selected member displays in a separate box.

    Visual Studio 中的 IntelliSense 清單成員

  2. 選取名為 [ Now] 的成員,也就是類別的屬性,方法是按兩下或按下 tab 鍵。在結尾加上分號,以完成程式程式碼。Select the member named Now, which is a property of the class, by double-clicking on it or pressing Tab. Complete the line of code by adding a semi-colon to the end.

  3. 在下方輸入或貼上下列程式碼:Below that, type in or paste the following lines of code:

    int dayOfYear = now.DayOfYear;
    
    Console.Write("Day of year: ");
    Console.WriteLine(dayOfYear);
    

    提示

    Console.WriteConsole.WriteLine 稍有不同,它不會在列印之後新增行結束字元。Console.Write is a little different to Console.WriteLine in that it doesn't add a line terminator after it prints. 這表示傳送至輸出的下一段文字會列印在同一行。That means that the next piece of text that's sent to the output will print on the same line. 您可以將滑鼠移至程式碼中每個方法上方來查看其描述。You can hover over each of these methods in your code to see their description.

  4. 接下來,我們將再使用一次重構,讓程式碼更精簡。Next, we'll use refactoring again to make the code a little more concise. 按一下 DateTime now = DateTime.Now; 行中的變數 nowClick on the variable now in the line DateTime now = DateTime.Now;.

    請注意,該行的邊界會出現一個小螺絲起子圖示。Notice that a little screwdriver icon appears in the margin on that line.

  5. 按一下螺絲起子圖示,以查看 Visual Studio 所提供的建議。Click the screwdriver icon to see what suggestions Visual Studio has available. 在此案例中,它會顯示內嵌暫存變數重構以移除程式碼,而不會變更程式碼的整體行為:In this case, it's showing the Inline temporary variable refactoring to remove a line of code without changing the overall behavior of the code:

    Visual Studio 中的內嵌暫存變數重構

  6. 按一下 [內嵌暫存變數] 以重構程式碼。Click Inline temporary variable to refactor the code.

  1. 按下 Ctrl + F5 再次執行程式。Run the program again by pressing Ctrl+F5. 輸出看起來會像這樣:The output looks something like this:

    !cmd.exe 主控台視窗的螢幕擷取畫面,其中顯示名稱、輸入和輸出「Hello Georgette!」的提示。!Screenshot of the cmd.exe console window showing the prompt for a name, the input, and the output 'Hello Georgette! 年中的日: 151 '.] (。/media/overview-console-final.png) Day of year: 151'.](../media/overview-console-final.png)

  1. 按下 Ctrl + F5 再次執行程式。Run the program again by pressing Ctrl+F5. 輸出看起來會像這樣:The output looks something like this:

    Microsoft Visual Studio 偵錯工具主控台視窗的螢幕擷取畫面,其中顯示名稱、輸入和輸出「Hello Georgette!」的提示。

偵錯程式碼Debug code

當您撰寫程式碼時,必須執行並測試它,以找出 Bug。When you write code, you need to run it and test it for bugs. Visual Studio 偵錯系統可讓您以一次一個陳述式的方式逐步偵錯程式碼,並一邊檢查變數。Visual Studio's debugging system lets you step through code one statement at a time and inspect variables as you go. 您可以設定「中斷點」,其會停止執行特定行的程式碼。You can set breakpoints that stop execution of the code at a particular line. 您可以觀察變數值如何隨著程式碼執行而變更,以及更多。You can observe how the value of a variable changes as the code runs, and more.

讓我們來設定中斷點,以查看程式「進行」期間的 username 變數值。Let's set a breakpoint to see the value of the username variable while the program is "in flight".

  1. 尋找程式碼行 Console.WriteLine($"\nHello {username}!");Find the line of code that says Console.WriteLine($"\nHello {username}!");. 若要設定這行程式碼的中斷點,讓程式在這行暫停執行,請按一下編輯器的左邊界。To set a breakpoint on this line of code, that is, to make the program pause execution at this line, click in the far left margin of the editor. 您也可以按一下該行程式碼的任何位置,然後按下 F9 鍵。You can also click anywhere on the line of code and then press F9.

    左邊界會出現一個紅色圓圈,並以紅色醒目顯示程式碼。A red circle appears in the far left margin, and the code is highlighted in red.

    Visual Studio 中程式碼行的中斷點

  2. 選取 [ Debug > 開始調試 程式] 或按下 F5 來開始進行偵錯工具。Start debugging by selecting Debug > Start Debugging or by pressing F5.

  3. 在主控台視窗出現並要求您的名稱時,鍵入名稱並按 Enter 鍵。When the console window appears and asks for your name, type it in and press Enter.

    焦點會返回 Visual Studio 程式碼編輯器,並以黃色醒目提示具有中斷點的程式碼。The focus returns to the Visual Studio code editor and the line of code with the breakpoint is highlighted in yellow. 這表示程式要執行的下一行程式碼。This signifies that it's the next line of code that the program will execute.

  4. 將滑鼠移至 username 變數上方以查看其值。Hover your mouse over the username variable to see its value. 或者,您可以在 username 上按一下滑鼠右鍵,然後選取 [新增監看式] 將變數新增至 [監看式] 視窗,您也可以在此查看其值。Alternatively, you can right-click on username and select Add Watch to add the variable to the Watch window, where you can also see its value.

    Visual Studio 偵錯期間的變數值

  5. 若要讓程式執行到完成,請再按一次 F5 鍵。To let the program run to completion, press F5 again.

如需有關 Visual Studio 中之 偵錯的更多詳細資料,請參閱偵錯工具功能導覽To get more details about debugging in Visual Studio, see Debugger feature tour.

自訂 Visual StudioCustomize Visual Studio

您可以個人化 Visual Studio 使用者介面,包括變更預設的色彩佈景主題。You can personalize the Visual Studio user interface, including change the default color theme. 若要變更為 [深色] 佈景主題:To change to the Dark theme:

  1. 在功能表列上,選擇 [工具 > 選項] 以開啟 [選項] 對話方塊。On the menu bar, choose Tools > Options to open the Options dialog.
  1. 在 [環境][一般] > 選項頁面上,將 [色彩佈景主題] 選項變更為 [深色],然後選擇 [確定]。On the Environment > General options page, change the Color theme selection to Dark, and then choose OK.

    整個 IDE 的色彩佈景主題會變更為 [深色]。The color theme for the entire IDE changes to Dark.

    深色佈景主題的 Visual Studio

  1. 在 [環境][一般] > 選項頁面上,將 [色彩佈景主題] 選項變更為 [深色],然後選擇 [確定]。On the Environment > General options page, change the Color theme selection to Dark, and then choose OK.

    整個 IDE 的色彩佈景主題會變更為 [深色]。The color theme for the entire IDE changes to Dark.

    深色佈景主題的 Visual Studio

若要了解您可以個人化 IDE 的其他方式,請參閱個人化 Visual StudioTo learn about other ways you can personalize the IDE, see Personalize Visual Studio.

下一步Next steps

遵循以下其中一篇簡介文章,進一步探索 Visual Studio:Explore Visual Studio further by following along with one of these introductory articles:

若您已準備好深入程式碼的世界,下一步就是開始下列其中一個語言專屬的快速入門:If you're ready to dive into more coding, one of the following language-specific quickstarts is a good next step:

另請參閱See also