Visual Studio 调试(系列文章)

  • 2019 年 10 月 4 日
  • 笔记

调试是软件开发过程中非常重要的一个部分,它具挑战性,但是也有一定的方法和技巧。

  Visual Studio 调试程序有助于你观察程序的运行时行为并发现问题。 该调试器可用于所有 Visual Studio 编程语言及其关联的库。 使用调试程序时,可以中断程序的执行以检查代码、检查和编辑变量、查看寄存器、查看从源代码创建的指令,以及查看应用程序占用的内存空间。

  本系列以 Visual Studio 2019 来演示调试的方法和技巧。希望能帮助大家掌握这些技巧。它们都很简单,却能帮你节约大量的时间。

调试方法与技巧

Visual Studio 调试系列1 Debug 与 Release 模式

Visual Studio 调试系列2 基本调试方法

Visual Studio 调试系列3 断点

Visual Studio 调试系列4 单步后退来检查旧应用状态(使用使用 IntelliTrace 窗口)

Visual Studio 调试系列5 检查变量(使用自动窗口和局部变量窗口)

Visual Studio 调试系列6 监视变量(使用监视窗口和快速监视窗口)

Visual Studio 调试系列7 查看变量占用的内存(使用内存窗口)

Visual Studio 调试系列8 查找导致程序崩溃的 DLL(使用模块窗口)

Visual Studio 调试系列9 调试器提示和技巧

Visual Studio 调试系列10 附加到正在运行的进程

Visual Studio 调试系列11 远程调试

Visual Studio 调试系列12 远程调试部署在远程计算机IIS上的ASP.NET应用程序

示例程序

后续的调试以下面的程序为示例进行演示说明。

  1 using System;    2 using System.Collections.Generic;    3    4 namespace Demo002_NF45_CS50    5 {    6     class Program    7     {    8         static void Main(string[] args)    9         {   10             var shapes = new List<Shape>   11             {   12                 new Triangle(4,3),   13                 new Rectangle(7,4),   14                 new Circle(),   15   16             };   17   18             foreach (var shape in shapes)   19             {   20                 shape.Width = 10;   21                 shape.Draw();   22   23                 int num1 = 2, num2 = 0;   24                 num1 = num1 / num2;   25   26                 Console.WriteLine();   27             }   28   29             Console.WriteLine("Press any key to exit."); // 在调试模式下保持控制台打开   30             Console.ReadKey();   31         }   32     }   33   34     #region 调试示例   35   36     /// <summary>   37     ///  绘图类(基类)   38     /// </summary>   39     public class Shape   40     {   41         #region 属性   42   43         /// <summary>   44         ///  X 轴坐标   45         /// </summary>   46         public int X { get; private set; }   47   48         /// <summary>   49         ///  Y 轴坐标   50         /// </summary>   51         public int Y { get; private set; }   52   53         /// <summary>   54         ///  图形高度   55         /// </summary>   56         public int Height { get; set; }   57   58         /// <summary>   59         ///  图形宽度   60         /// </summary>   61         public int Width { get; set; }   62   63         #endregion   64   65         // 绘制图形(虚方法)   66         public virtual void Draw()   67         {   68             Console.WriteLine("Performing base class drawing tasks");// 执行基类绘图任务   69         }   70     }   71   72     /// <summary>   73     ///  圆形   74     /// </summary>   75     class Circle : Shape   76     {   77         public override void Draw()   78         {   79             Console.WriteLine("Drawing a circle"); // 绘制一个圆   80             base.Draw();   81         }   82     }   83   84     /// <summary>   85     ///  矩形   86     /// </summary>   87     class Rectangle : Shape   88     {   89         public Rectangle()   90         {   91   92         }   93   94         public Rectangle(int width, int height)   95         {   96             Width = width;   97             Height = height;   98         }   99  100         public override void Draw()  101         {  102             Console.WriteLine("Drawing a rectangle"); // 绘制一个矩形  103             base.Draw();  104         }  105     }  106  107     /// <summary>  108     ///  三角形  109     /// </summary>  110     class Triangle : Shape  111     {  112         public Triangle()  113         {  114  115         }  116  117         public Triangle(int width, int height)  118         {  119             Width = width;  120             Height = height;  121         }  122  123         public override void Draw()  124         {  125             Console.WriteLine("Drawing a trangle");// 绘制一个三角形  126             base.Draw();  127         }  128     }  129  130     #endregion  131 }