欢迎使用 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. 解决方案资源管理器可将代码文件分组为解决方案和项目,从而帮助整理代码 。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. 本文重点介绍 Visual Studio 2017 的 Windows 版本。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. 本文重点介绍 Visual Studio 2019 的 Windows 版本。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 中重构

  • IntelliSenseIntelliSense

    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 可帮助查找代码引用、代码更改、链接错误、工作项、代码评审和单元测试,所有操作都在编辑器上进行。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() 方法在控制台窗口中It's a simple "Hello World" application that calls the Console.WriteLine() method to display the literal string "Hello World!" 显示文本字符串“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+F5 。You 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!

    cmd.exe 控制台窗口的屏幕截图,其中显示输出“Hello Word!”

  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. 在控制台窗口中输入姓名,并按 Enter。Enter 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“启动”窗口创建新项目

    随即打开“创建新项目”窗口,并显示几个项目模板。The Create a new project window opens and shows several project templates. 模板包含给定项目类型所需的基本文件和设置。A template contains the basic files and settings needed 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#”进一步筛选模板结果。You can further filter the template results by choosing C# from the Language drop-down list. 选择“控制台应用 (.NET Core)”模板,然后选择“下一步”。Select the Console App (.NET Core) template, and then choose Next.

    在 Visual Studio 中创建新项目

  4. 在“配置新项目”窗口中,在“项目名称”框中输入“HelloWorld”,根据需要更改项目文件的目录位置,然后选择“创建”。In the Configure your new project window, enter HelloWorld in the Project name box, optionally change the directory location for your project files, and then choose Create.

    在 Visual Studio 中配置新项目

    Visual Studio 随即创建项目。Visual Studio creates the project. 它是简单的“Hello World”应用程序,可调用 Console.WriteLine() 方法在控制台窗口中It's a simple "Hello World" application that calls the Console.WriteLine() method to display the literal string "Hello World!" 显示文本字符串“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.

  5. 现在启动该应用。Now, start the app. 可从菜单栏的“调试”菜单中选择“开始执行(不调试)”,以执行此操作。You can do this by choosing Start Without Debugging from the Debug menu on the menu bar. 还可按 Ctrl+F5 。You 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!”

  6. 要关闭控制台窗口,请在键盘上按任意键。To close the console window, press any key on your keyboard.

  7. 接下来,向应用添加一些附加代码。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.

  8. 将显示 Console.WriteLine("Hello World!"); 的行更改为以下代码:Change the line that says Console.WriteLine("Hello World!"); to the following code:

    Console.WriteLine($"\nHello {name}!");
    
  9. 选择“调试”“开始执行(不调试)” > ,或按 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.

  10. 在控制台窗口中输入姓名,并按 Enter。Enter your name in the console window and press Enter.

    Microsoft Visual Studio 调试控制台窗口的屏幕截图,其中显示输入姓名的提示、所输入的姓名以及输出“Hello Georgette!”。

  11. 按任意键关闭控制台窗口,并停止正在运行的程序。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. 为变量 username 键入新名称。Type 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. 通过双击或按 Tab 选择名为“Now”(该类的一个属性)的成员 。通过向末尾添加分号来完成代码行。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; 行中的 now 变量。Click 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! Day of year: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. 选择“调试” > “启动调试”或按 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