在 Visual Studio 中进行调试的基础知识

已完成

调试基础知识

编写代码很难做到无错误。 可使用 Visual Studio 查找项目中的 bug。

简单调试

向项目添加 print 语句以查看代码的执行方式。 未有效执行的代码可能会导致 bug。 使用此方法对较小的项目进行简单的调试。 除了简单的项目外,不推荐使用此方法

print 语句添加到通用 Windows 平台 (UWP) 项目:

  1. 在“解决方案资源管理器”中,展开“MainPage.xaml”,然后选择“MainPage.xaml.cs”

    Screenshot of the Visual Studio solution explorer. The MainPage.xaml.cs file is highlighted.

    此处的 C# 代码与应用中显示的窗口相关联。

  2. 在“编辑器窗口”中向下滚动,直到找到以下代码

    public MainPage()
    {
      this.InitializeComponent();
    }
    

    将其更改为以下代码:

    public MainPage()
    {
      this.InitializeComponent();
      Hello();
    }
    
    public void Hello()
    {
      System.Diagnostics.Debug.WriteLine("Hello!");
    }
    

    “编辑器窗口”应如下所示

    Screenshot of the editor window in Visual Studio. Sample code from the previous WriteLine steps is shown.

  3. 选择“运行”按钮或按 F5 键运行项目

    Screenshot showing the Visual Studio menu bar. The run button, represented by a green triangle, is highlighted.

  4. 最小化应用窗口,返回到 Visual Studio。 选择“输出”以查看应用创建的“Hello!”消息

    Screenshot showing the Visual Studio output window with the Hello! message shown.

    注意

    还可以在 Windows Presentation Foundation (WPF) 和 Windows 窗体应用中显示调试文本

  5. 选择“停止”按钮以停止应用

    Screenshot showing the Visual Studio menu bar. The stop button, represented by a red square, is highlighted.

优化调试

使用断点在特定点停止应用,以了解发生的状况。 让我们看看它的工作原理。

  1. 更新 Hello() 函数以匹配此代码:

    public void Hello()
    {
      int a = 1;
      int b = 2;
      int c = a + b;
    
      if (c == 4)
      {
          // Success
      }
      else
      {
          // Fail
      }
    }
    

    “编辑器窗口”应如下所示

    Screenshot of the editor window in Visual Studio. Sample code from the above breakpoint steps is shown.

    说明:应用程序需要 4 的值来执行 Hello() 函数。 遗憾的是,在这种情况下,a + b 不等于 c。 让我们检查使用断点执行此计算的行。

  2. 在包含代码 int c = a + b; 的行旁边的灰色边距内选择。 将显示一个红点。 这个点就是断点。

    Screenshot of the Visual Studio editor window. A breakpoint (red circle) has been placed in the margin.

  3. 选择“运行”按钮或按 F5 再次启动应用

    Screenshot showing the Visual Studio menu bar. The run button, represented by a green triangle, is highlighted.

    应用立即停止运行。 边距内会出现一个黄色的小箭头,在包含错误的行旁边。 突出显示的代码行是接下来运行的代码。

    Screenshot of the Visual Studio editor window. A yellow arrow is displayed within the breakpoint in the margin.

    当程序暂停时,可使用工具栏上的“单步执行”、“单步跳过”和“单步跳出”按钮逐行单步调试程序

    Screenshot of the Visual Studio menu bar. The step into, step over, and step out options are highlighted.

  4. 选择“单步执行”,并观察黄色箭头随控制流的变化情况

    Screenshot of the Visual Studio menu bar. The step into option is highlighted.

  5. 将鼠标悬停在 c 变量上。

    Screenshot of the Visual Studio editor window. A pop-up displays where the mouse was hovering. It indicates c has a value of three.

    此时会显示一个窗口以显示变量的当前值。 正如我们所知,该值为 3,而不是函数预期的 4。

  6. 将鼠标悬停在右大括号附近,直到出现绿色箭头。

    Screenshot of the Visual Studio editor window. A green arrow is displayed where the mouse was hovering.

    此断点允许应用运行,直到到达包含指定代码的行。

  7. 选择“停止”按钮停止运行应用

    Screenshot showing the Visual Studio menu bar. The stop button, represented red square, is highlighted.

调试理念

简单介绍一下调试。 了解你掌握的工具是成功的一半。 理解为什么有些内容不起作用需要经验、耐心和运气。 使用这些提示调试代码:

  • 需要明确的是,代码完全在按照你的要求执行操作。 你只是要求代码执行了错误的操作。
  • 向朋友(甚至是你自己)逐行解释你的代码。 大声说出来会有所帮助。
  • 将代码分解为更小的部分(一种重构形式),以确认每个部分是否都能正常运行。
  • 有时休息一下,理清思绪会有所帮助。