You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Check what you learned about debugging in Visual Studio.
7
7
author: alexwolfmsft
8
8
ms.author: alexwolf
9
9
manager: jmartens
@@ -33,36 +33,36 @@ quiz:
33
33
choices:
34
34
- content: '`Debug.Assert(count != 0, "Count should not be 0.");`'
35
35
isCorrect: false
36
-
explanation: "Assert tests a condition. If it's true, nothing will happen or else the application will enter break mode. So here, if the count is 0, it will enter break mode."
36
+
explanation: "Assert tests a condition. If it's true, nothing happens or else the application enters break mode. So here, if the count is 0, it enters break mode."
37
37
- content: '`Debug.Assert(count == 0, "Count should not be 0.");`'
38
38
isCorrect: false
39
-
explanation: "Assert tests a condition. If it's true, nothing will happen or else the application will enter break mode. So here, if the count is 0, nothing will happen."
39
+
explanation: "Assert tests a condition. If it's true, nothing happens or else the application enters break mode. So here, if the count is 0, nothing happens."
40
40
- content: '`Debug.WriteIf(count != 0, "Count should not be 0.");`'
41
41
isCorrect: false
42
42
explanation: "This code prints a message when the count doesn't equal 0."
43
43
- content: '`Debug.WriteIf(count == 0, "Count should not be 0.");`'
44
44
isCorrect: true
45
-
explanation: "This code will only print a debug message when the count is 0."
45
+
explanation: "This code only prints a debug message when the count is 0."
46
46
- content: "What are the top two values a debugger provides?"
47
47
choices:
48
48
- content: "Control of your program execution and observation of your program's state"
49
49
isCorrect: true
50
50
explanation: "The two main values almost every debugger provides are the ability to control program execution and to observe program state."
51
-
- content: "Modifying program values and changing your program output"
51
+
- content: "Modifying program values and changing your program output."
52
52
isCorrect: false
53
-
explanation: "Changing your program's output isn't useful because it will only have effect when the application is being debugged."
54
-
- content: "Observing your program's state and modifying your program values"
53
+
explanation: "Changing your program's output isn't useful because it only has effect when the application is being debugged."
54
+
- content: "Observing your program's state and modifying your program values."
55
55
isCorrect: false
56
56
explanation: "Observing and modifying values are important, but it's also important to be able to control the flow of program execution."
57
-
- content: "Editing your program while running and editing program values"
57
+
- content: "Editing your program while running and editing program values."
58
58
isCorrect: false
59
59
explanation: "Editing programs is useful, but observing your program's operational state is another important value that the .NET debugger provides."
60
60
- content: "Which Visual Studio debugger window is most useful to observe the current value of a specific variable across different functions?"
61
61
choices:
62
62
- content: "Use the Locals window because it shows all variables that are currently in scope."
63
63
isCorrect: false
64
64
explanation: "While the Locals window does display all variables currently in scope, it's not the best way to watch the value of a specific variable."
65
-
- content: "Hover over the variable in the code editor to display the value."
65
+
- content: "Display the value by hovering over the variable in the code editor."
66
66
isCorrect: false
67
67
explanation: "While hovering does display the variable's current value, it's a manual process and isn't efficient."
68
68
- content: "Use the Watch window to select specific variables or expressions to watch throughout the program's execution."
Copy file name to clipboardExpand all lines: learn-pr/language/dotnet-debug-visual-studio/includes/3-analyze-your-program-state.md
+19-21Lines changed: 19 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ Let's start by learning how to use the Visual Studio debugger with .NET.
4
4
5
5
## Getting started with the Visual Studio Debugger
6
6
7
-
Use the **Start** button in the Visual Studio command bar to launch the application with the debugger attached. Notice that the command bar now includes our project name. Once the app is running, more debugging tools and features become available.
7
+
Use the **Start** button (the solid green triangle) in the Visual Studio command bar to launch the application with the debugger attached. Notice that the command bar now includes our project name. Once the app is running, more debugging tools and features become available.
8
8
9
9
:::image source="../media/visual-studio-debugging-controls.png" alt-text="Screenshot of Visual Studio debugging controls.":::
10
10
@@ -15,21 +15,19 @@ Use the **Start** button in the Visual Studio command bar to launch the applicat
15
15
16
16
### Controlling execution
17
17
18
-
Once the app is running, the debugging controls appear next to the **Start** button you clicked to launch the app.
18
+
Once the app is running, the debugging controls appear next to the **Start** button that you clicked to launch the app.
19
19
20
20
:::image source="../media/visual-studio-steps.png" alt-text="Screenshot of Visual Studio debugger execution controls.":::
21
21
22
-
The controls are numbered as follows:
23
-
24
22
-**1) Continue or pause execution**. If execution is paused on a breakpoint, select this button to continue until the next breakpoint is hit. If your program is running, the button switches to a pause button that you can use to pause execution.
25
-
-**2) Hot reload controls**. This new feature of Visual Studio 2022 allows you to make changes and refresh our code without restarting the app.
23
+
-**2) Hot reload controls**. This feature, introduced in Visual Studio 2022, allows you to make changes and refresh your code without restarting the app.
26
24
-**3) Stop**. This button stops the running application, which also detaches the debugger.
27
25
-**4) Restart**. Stops and relaunches the app with the debugger attached.
28
26
-**5) Step into**. If the next statement is a function call, move down into the first code statement of this function (same as the `step` command).
29
27
-**6) Step over**. If the next statement is a function call, execute the code, but move on immediately to the next line of code in the current function.
30
28
-**7) Step out**. If you're inside a function, execute the remaining code of this function and jump back to the statement after the initial function call (same as the `out` command).
31
29
32
-
Breakpoints are one of the core features of debugging and integrate with these controls, so let's explore them in more detail.
30
+
Breakpoints are one of the core features of debugging and they integrate with these controls, so let's explore them in more detail.
33
31
34
32
## Breakpoints
35
33
@@ -39,33 +37,33 @@ You can add a breakpoint in Visual Studio by clicking to the left side of the li
39
37
40
38
:::image source="../media/visual-studio-breakpoint.png" alt-text="Screenshot of a breakpoint added in the Visual Studio editor window.":::
41
39
42
-
If you right-click to add a breakpoint or on an existing breakpoint, you can also select **Add Conditional Breakpoint**. This special type of breakpoint allows you to define a *condition* or criteria for when the breakpoint is triggered. This menu also allows you to select **only Enable when following breakpoint is hit option** to create a chainable breakpoints execution, among other actions.
40
+
If you right-click to add a breakpoint, or right-click on an existing breakpoint, you can also select **Add Conditional Breakpoint**. This special type of breakpoint allows you to define a *condition* or criteria for when the breakpoint is triggered. This menu also allows you to select the **Only Enable when the following breakpoint is hit** option to create a chainable breakpoints execution, among other actions.
43
41
44
42
:::image source="../media/visual-studio-breakpoint-conditional.png" alt-text="Screenshot of setting a conditional breakpoint in Visual Studio.":::
45
43
46
-
You can also use a Temporary breakpoint if you want to break the code just one time. You can apply Temporary breakpoints by right-clicking in the breakpoint column and selecting **Insert temporary breakpoint**. The Temporary breakpoints will be removed after they're hit the first time.
44
+
You can also use a *temporary breakpoint* if you want to break the code just one time. You can apply temporary breakpoints by right-clicking in the breakpoint column and selecting **Insert Temporary Breakpoint**. The Temporary breakpoints will be removed after they're hit the first time.
47
45
48
-
You can also add Tracepoints by right-clicking in the breakpoint column. Tracepoints allow you to log information to the Output window under configurable conditions without modifying or stopping your code execution.
46
+
You can also add *tracepoints* by right-clicking in the breakpoint column and selecting **Insert Tracepoint**. Tracepoints allow you to log information to the Output window under configurable conditions without modifying or stopping your code execution.
49
47
50
48
Finally, in the **Breakpoints** window (**Debug** > **Windows** > **Breakpoints**), you can see and toggle all the breakpoints you placed in your code. You can also toggle options to break on caught or uncaught exceptions. You can use the **Breakpoints** panel to examine your program state and trace back the source of an exception by using the **Call stack** when one occurs.
51
49
52
50
:::image source="../media/visual-studio-breakpoints-window.png" alt-text="Screenshot of the Visual Studio Breakpoints window.":::
53
51
54
52
## Visual Studio debugging tools
55
53
56
-
After you've set up your breakpoints and started your app, new information windows and controls appear on the screen.
54
+
After you set up your breakpoints and start your app, new information windows and controls appear on the screen.
57
55
58
56
:::image source="../media/visual-studio-controls.png" alt-text="Screenshot of Visual Studio debugger overview.":::
59
57
60
-
1. Debugger launch controls
61
-
1.Current breakpoint and execution line
62
-
1. Watch window to monitor variable values
63
-
1. Breakpoints window to view all breakpoints
58
+
1. Debugger launch controls.
59
+
1.The current breakpoint and execution line.
60
+
1.**Watch window** for monitoring variable values.
61
+
1.**Breakpoints window** for viewing all breakpoints.
64
62
65
63
You can also access other helpful debugging windows by selecting **Debug** > **Windows** along the top navigation bar. For example, commonly used tools include:
66
64
67
-
1.**Call Stack window**: Allows you to view which methods have been called.
68
-
2.**Immediate window**: Allows you to write and expressions while debugging.
65
+
1.**Call Stack window**: Allows you to view which methods were called.
66
+
2.**Immediate window**: Allows you to write and evaluate expressions while debugging.
69
67
3.**Autos window**: Automatically adds watches to variables in the current context.
70
68
4.**Output window**: Shows the output of logging statements or code that writes to the console.
71
69
@@ -84,17 +82,17 @@ When you analyze the cause of a program defect, you can watch the state of your
84
82
85
83
Most of these windows also allow you to double-click the value of a variable and change its value while debugging.
86
84
87
-
The **Watch**windows are useful for tracking variables during the execution of your code. You can right-click a variable in your editor and select **Add to watch**. That variable now displays in the watch window and updates automatically as your app executes.
85
+
The **Watch**window is useful for tracking variables during the execution of your code. You can right-click a variable in your editor and select **Add to watch**. That variable now displays in the watch window and updates automatically as your app executes.
88
86
89
87
You can also right-click variables in the **Autos** or **Locals** windows to add a watch.
90
88
91
-
Another productive way to view and analyze the variable information during debugging is using DataTips. When you pause at the breakpoint, hover over any variable in the current scope. A DataTip appears, showing the name and current value of the variable properties. By hovering over a function parameter or a variable directly in the editor window, you can also peek at its value.
89
+
Another productive way to view and analyze the variable information during debugging is by using *DataTips*. When you pause at the breakpoint, hover over any variable in the current scope. A DataTip appears, showing the name and current value of the variable properties. By hovering over a function parameter or a variable directly in the editor window, you can also peek at its value.
92
90
93
91
### Call stack
94
92
95
93
Every time your program enters a function, an entry is added to the call stack. When your application becomes complex and functions are called within other functions many times, the call stack represents the trail of those calls.
96
94
97
-
It's useful to find the source of an exception. If you have an unexpected crash in your program, you often see something in the console like the following example:
95
+
It's useful for finding the source of an exception. If you have an unexpected crash in your program, you often see something in the console like the following example:
98
96
99
97
```text
100
98
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
@@ -104,11 +102,11 @@ Unhandled exception. System.IndexOutOfRangeException: Index was outside the boun
104
102
105
103
The group of `at [...]` lines under the error message is called a *stack trace*. The stack trace gives the name and origin of every function that was called before ending up with the exception. However, it can be a bit difficult to decipher, because it also includes internal functions from the .NET runtime.
106
104
107
-
The Visual Studio **Call stack** window comes in handy here. It filters out unwanted information to show you only the relevant functions from your own code by default. You then can unwind this call stack to find out where the exception originated from.
105
+
The Visual Studio **Call stack** window comes in handy here. It filters out unwanted information to show you only the relevant functions from your own code by default. You can then unwind this call stack to find out where the exception originated from.
108
106
109
107
:::image source="../media/visual-studio-callstack.png" alt-text="Screenshot of the Call Stack window.":::
110
108
111
-
In the next unit, you'll walk through an exercise using the debugger to fix the bug in the Fibonacci code we saw previously.
109
+
In the next unit, you walk through an exercise using the debugger to fix the bug in the Fibonacci code we saw previously.
0 commit comments