Debugging allows developers to see step by step how the code works, how the values of variables change, how objects are created and destroyed, etc.
When a web page is run for the first time, Visual Studio will pop up a prompt box asking whether Debugging needs to be enabled:
When debugging is enabled, the following lines of code will appear in the web.config file:
<system.web> <compilation debug="true"> <assemblies> ............ </assemblies> </compilation> </system.web>
The Debugging toolbar will provide all the tools needed for debugging:
Breakpoints specify that the program should stop running immediately after running the specified line of code during runtime, so that the code can be tested and various debugging tasks can be completed, such as observing changes in variable values, single-step debugging of code, and jumps to function methods. In and out, etc. Set a breakpoint by right-clicking on the code and selecting Insert a Breakpoint. Then a red dot will appear on the left and the line of code will be highlighted, as shown in the figure:
After you run this code, you will observe the breakpoint behavior.
At this stage, you can step through the code and observe the running process and variable values, properties, objects, etc.
If you need to modify the breakpoint properties, you can right-click on the breakpoint mark and find it in the "Properties" menu:
The location dialog box displays the location of the file, as well as the number of lines and characters of the selected code. The condition menu allows you to enter a valid expression to evaluate whether the program has reached a breakpoint:
The Hit Count menu displays a dialog box showing the number of times the breakpoint has been hit.
Clicking on any option in the drop-down menu will open an edit box for entering the number of hits. This is useful when analyzing code with loop structures.
The Filter menu allows you to set a filter for a specific mechanism, process, thread, or any combination to apply breakpoints to.
The When Hit menu allows you to specify actions when a breakpoint is hit.
Visual Studio provides the following debug windows, each of which displays some program information. The following table lists some windows:
window | describe |
---|---|
direct | Display variables and expressions. |
automatic | Displays all current variables and their previous state. |
local | Displays all variables of the current context. |
observe | Display variables from up to four different collections. |
call stack | Display all methods in the call stack. |
thread | Display and control threads. |