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
Copy file name to clipboardExpand all lines: docs/debugger/assertions-in-managed-code.md
+16-16Lines changed: 16 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -73,14 +73,14 @@ An assertion, or `Assert` statement, tests a condition, which you specify as an
73
73
## <aname="BKMK_The_Debug_Assert_method"></a> The Debug.Assert method
74
74
Use the <xref:System.Diagnostics.Debug.Assert%2A?displayProperty=fullName> method freely to test conditions that should hold true if your code is correct. For example, suppose you have written an integer divide function. By the rules of mathematics, the divisor can never be zero. You might test this using an assertion:
@@ -90,29 +90,29 @@ int IntegerDivide ( int dividend , int divisor )
90
90
91
91
Here is another example. You have a class that implements a checking account, as follows:
92
92
93
-
```vb
93
+
```VB
94
94
Dimamount,balanceAsDouble
95
95
balance=savingsAccount.balance
96
96
Debug.Assert(amount<=balance)
97
97
SavingsAccount.Withdraw(amount)
98
98
```
99
99
100
-
```c#
100
+
```CSharp
101
101
floatbalance=savingsAccount.Balance;
102
102
Debug.Assert ( amount<=balance );
103
103
savingsAccount.Withdraw ( amount );
104
104
```
105
105
106
106
Before you withdraw money from the account, you want to make sure that the account balance is sufficient to cover the amount you are preparing to withdraw. You might write an assertion to check the balance:
## <aname="BKMK_Side_effects_of_Debug_Assert"></a> Side effects of Debug.Assert
128
128
When you use <xref:System.Diagnostics.Debug.Assert%2A?displayProperty=fullName>, make sure that any code inside `Assert` does not change the results of the program if `Assert` is removed. Otherwise, you might accidentally introduce a bug that only shows up in the Release version of your program. Be especially careful about asserts that contain function or procedure calls, such as the following example:
129
129
130
-
```vb
130
+
```VB
131
131
' unsafe code
132
132
Debug.Assert(meas(i)<>0)
133
133
```
134
134
135
-
```c#
135
+
```CSharp
136
136
// unsafe code
137
137
Debug.Assert (meas(i) !=0 );
138
138
```
139
139
140
140
This use of <xref:System.Diagnostics.Debug.Assert%2A?displayProperty=fullName> might appear safe at first glance, but suppose the function meas updates a counter each time it is called. When you build the Release version, this call to meas is eliminated, so the counter does not get updated. This is an example of a function with a side effect. Eliminating a call to a function that has side effects could result in a bug that only appears in the Release version. To avoid such problems, do not place function calls in a <xref:System.Diagnostics.Debug.Assert%2A?displayProperty=fullName> statement. Use a temporary variable instead:
<xref:System.Diagnostics.Trace.Assert%2A?displayProperty=fullName> and <xref:System.Diagnostics.Debug.Assert%2A?displayProperty=fullName> take up to three arguments. The first argument, which is mandatory, is the condition you want to check. If you call <xref:System.Diagnostics.Trace.Assert(System.Boolean)?displayProperty=fullName> or <xref:System.Diagnostics.Debug.Assert(System.Boolean)?displayProperty=fullName> with only one argument, the `Assert` method checks the condition and, if the result is false, outputs the contents of the call stack to the **Output** window. The following example shows <xref:System.Diagnostics.Trace.Assert(System.Boolean)?displayProperty=fullName> and <xref:System.Diagnostics.Debug.Assert(System.Boolean)?displayProperty=fullName>:
177
177
178
-
```vb
178
+
```VB
179
179
Debug.Assert(stacksize>0)
180
180
Trace.Assert(stacksize>0)
181
181
```
182
182
183
-
```c#
183
+
```CSharp
184
184
Debug.Assert ( stacksize>0 );
185
185
Trace.Assert ( stacksize>0 );
186
186
```
187
187
188
188
The second and third arguments, if present, must be strings. If you call <xref:System.Diagnostics.Trace.Assert%2A?displayProperty=fullName> or <xref:System.Diagnostics.Debug.Assert%2A?displayProperty=fullName> with two or three arguments, the first argument is a condition. The method checks the condition and, if the result is false, outputs the second string and third strings. The following example shows <xref:System.Diagnostics.Debug.Assert(System.Boolean,System.String)?displayProperty=fullName> and <xref:System.Diagnostics.Trace.Assert(System.Boolean,System.String)?displayProperty=fullName> used with two arguments:
189
189
190
-
```vb
190
+
```VB
191
191
Debug.Assert(stacksize>0,"Out of stack space")
192
192
Trace.Assert(stacksize>0,"Out of stack space")
193
193
```
194
194
195
-
```c#
195
+
```CSharp
196
196
Debug.Assert ( stacksize>0, "Out of stack space" );
197
197
Trace.Assert ( stacksize>0, "Out of stack space" );
198
198
```
199
199
200
200
The following example shows <xref:System.Diagnostics.Debug.Assert%2A> and <xref:System.Diagnostics.Trace.Assert%2A>:
201
201
202
-
```vb
202
+
```VB
203
203
Debug.Assert(stacksize>0,"Out of stack space. Bytes left:",Format(size,"G"))
204
204
Trace.Assert(stacksize>0,"Out of stack space. Bytes left:",Format(size,"G"))
205
205
Trace.Assert(stacksize>0,"Out of stack space. Bytes left:","inctemp failed on third call")
206
206
```
207
207
208
-
```c#
208
+
```CSharp
209
209
Debug.Assert ( stacksize>100, "Out of stack space" , "Failed in inctemp" );
210
210
Trace.Assert ( stacksize>0, "Out of stack space", "Failed in inctemp" );
title: "Inspect Variables in the Debugger | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "02/07/2017"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology:
@@ -11,12 +11,6 @@ ms.topic: "hero-article"
11
11
f1_keywords:
12
12
- "vs.debug.autos"
13
13
- "vs.debug.locals"
14
-
dev_langs:
15
-
- "FSharp"
16
-
- "VB"
17
-
- "CSharp"
18
-
- "C++"
19
-
- "JScript"
20
14
helpviewer_keywords:
21
15
- "debugger, variable windows"
22
16
- "debugging [Visual Studio], variable windows"
@@ -44,49 +38,49 @@ translation.priority.mt:
44
38
# Inspecting Variables in the Autos and Locals Windows
45
39
The **Autos** window (while debugging, **CTRL+ALT+V, A**, or **Debug / Windows / Autos**) and the **Locals** window (while debugging, **CTRL+ALT+V, L**, or **Debug / Windows / Locals**) are quite useful when you want to see variable values while you are debugging. The **Locals** window displays variables that are defined in the local scope, which is generally the function or method that is currently being executed. The **Autos** window displays variables used around the current line (the place where the debugger is stopped). Exactly which variables displayed is different in different languages. See What variables appear in the Autos Window? below.
46
40
47
-
If you need more information about basic debugging, see [Getting Started with the Debugger](../debugger/getting-started-with-the-debugger.md).
41
+
If you need more information about basic debugging, see [Getting Started with the Debugger](../debugger/getting-started-with-the-debugger.md).
48
42
49
43
## Looking at objects in the Autos and Locals windows
50
-
Arrays and objects are displayed in the Autos and Locals windows as tree controls. Click on the arrow to the left of the variable name to expand the view to show fields and properties. Here is an example of a [FileStream](http://msdn.microsoft.com/Library/a8737776-e545-4867-91ed-51c7f031fa19) object in the **Locals** window:
44
+
Arrays and objects are displayed in the Autos and Locals windows as tree controls. Click on the arrow to the left of the variable name to expand the view to show fields and properties. Here is an example of a [FileStream](http://msdn.microsoft.com/Library/a8737776-e545-4867-91ed-51c7f031fa19) object in the **Locals** window:
You can use the **Autos** window in C#, Visual Basic, and C++ code. The **Autos** window does not support JavaScript or F#.
56
50
57
-
In C# and Visual Basic, the **Autos** window displays any variable used on the current or preceding line. For example, if you declare four variables and set them as follows:
58
-
59
-
```c#
60
-
publicstaticvoidMain()
61
-
{
62
-
inta, b, c, d;
63
-
a=1;
64
-
b=2;
65
-
c=3;
66
-
d=4;
67
-
}
68
-
```
69
-
51
+
In C# and Visual Basic, the **Autos** window displays any variable used on the current or preceding line. For example, if you declare four variables and set them as follows:
52
+
53
+
```CSharp
54
+
publicstaticvoidMain()
55
+
{
56
+
inta, b, c, d;
57
+
a=1;
58
+
b=2;
59
+
c=3;
60
+
d=4;
61
+
}
62
+
```
63
+
70
64
If you set a breakpoint on the line `c = 3`; and run the debugger, when execution stops the **Autos** window will look like this:
Note that the value of `c` is 0, because the line `c = 3` has not yet been executed.
75
-
76
-
In C++ the **Autos** window displays the variables used at least three lines before the current line (the line at which execution is stopped). If you declare six variables:
77
-
78
-
```cpp
79
-
voidmain() {
80
-
int a, b, c, d, e, f;
81
-
a = 1;
82
-
b = 2;
83
-
c = 3;
84
-
d = 4;
85
-
e = 5;
86
-
f = 6;
87
-
}
88
-
```
89
-
69
+
70
+
In C++ the **Autos** window displays the variables used at least three lines before the current line (the line at which execution is stopped). If you declare six variables:
71
+
72
+
```C++
73
+
voidmain() {
74
+
int a, b, c, d, e, f;
75
+
a = 1;
76
+
b = 2;
77
+
c = 3;
78
+
d = 4;
79
+
e = 5;
80
+
f = 6;
81
+
}
82
+
```
83
+
90
84
If you set a breakpoint on the line `e = 5;` and run the debugger, when execution stops the **Autos** window will look like this:
In .NET and C++ code you can examine return values when you step over or out of a method call. This functionality is useful when the result of a method call is not stored in a local variable, for example when a method is used as a parameter or as a return value of another method.
100
94
101
95
The following C# code adds the return values of two functions:
102
-
103
-
```c#
96
+
97
+
```CSharp
104
98
static void Main(string[] args)
105
99
{
106
100
int a, b, c, d;
@@ -120,29 +114,28 @@ private static int subtractVars(int i, int j)
120
114
{
121
115
return j - i;
122
116
}
123
-
124
-
```
125
-
117
+
```
118
+
126
119
Set a breakpoint on the int `x = sumVars(a, b) + subtractVars(c, d);` line.
127
120
128
121
Start debugging, and when execution breaks at the first breakpoint, press **F10 (Step Over)**. You should see the following in the **Autos** window:
## Why are variable values sometimes red in Locals and Autos windows?
133
-
You may notice that the value of a variable is sometimes red in the **Locals** and **Autos** windows. These are variable values that have been changed since the last evaluation. The change could be from a previous debugging session, or because the value was changed in the window.
126
+
You may notice that the value of a variable is sometimes red in the **Locals** and **Autos** windows. These are variable values that have been changed since the last evaluation. The change could be from a previous debugging session, or because the value was changed in the window.
134
127
135
128
## Changing the numeric format of a variable window
136
-
The default numeric format is decimal, but you can change it to hexadecimal. Right-click inside a **Locals** or **Autos** window and select **Hexadecimal Display**. The change affects all debugger windows.
129
+
The default numeric format is decimal, but you can change it to hexadecimal. Right-click inside a **Locals** or **Autos** window and select **Hexadecimal Display**. The change affects all debugger windows.
137
130
138
131
## Editing a Value in a Variable Window
139
-
You can edit the values of most variables that appear in the **Autos**, **Locals**, **Watch**, and **QuickWatch** windows. For information about **Watch** and **QuickWatch** windows, see [Watch and QuickWatch Windows](../debugger/watch-and-quickwatch-windows.md). Just double-click the value you want to change and add the new the value.
140
-
141
-
You can enter an expression for a value, for example `a + b`. The debugger accepts most valid language expressions.
132
+
You can edit the values of most variables that appear in the **Autos**, **Locals**, **Watch**, and **QuickWatch** windows. For information about **Watch** and **QuickWatch** windows, see [Watch and QuickWatch Windows](../debugger/watch-and-quickwatch-windows.md). Just double-click the value you want to change and add the new the value.
142
133
143
-
In native C++ code, you might have to qualify the context of a variable name. For more information, see [Context Operator (C++)](../debugger/context-operator-cpp.md).
134
+
You can enter an expression for a value, for example `a + b`. The debugger accepts most valid language expressions.
144
135
145
-
However, you should exercise caution when changing values. Here are some possible issues:
136
+
In native C++ code, you might have to qualify the context of a variable name. For more information, see [Context Operator (C++)](../debugger/context-operator-cpp.md).
137
+
138
+
However, you should exercise caution when changing values. Here are some possible issues:
146
139
147
140
- Evaluating some expressions can change the value of a variable or otherwise affect the state of your program. For example, evaluating `var1 = ++var2` changes the value of `var1` and `var2`.
148
141
@@ -151,11 +144,11 @@ private static int subtractVars(int i, int j)
151
144
- Editing floating-point values can result in minor inaccuracies because of decimal-to-binary conversion of fractional components. Even a seemingly harmless edit can result in changes to some of the least significant bits in the floating-point variable.
152
145
153
146
## Debug Location toolbar
154
-
You can use the **Debug Location** toolbar to select the desired function, thread, or process. Set a breakpoint and start debugging. (If you do not see this toolbar, you can enable it by clicking in an empty part of the toolbar area. You should see a list of toolbars; select **Debug Location**). When the breakpoint is hit, execution stops and you can see the Debug Location toolbar, which is the bottom row of the following graphic:
147
+
You can use the **Debug Location** toolbar to select the desired function, thread, or process. Set a breakpoint and start debugging. (If you do not see this toolbar, you can enable it by clicking in an empty part of the toolbar area. You should see a list of toolbars; select **Debug Location**). When the breakpoint is hit, execution stops and you can see the Debug Location toolbar, which is the bottom row of the following graphic:
You can also change the context to different function calls, threads, or processes by double-clicking the element in the **Call Stack** window, the **Threads** window, or the **Processes** window.
151
+
You can also change the context to different function calls, threads, or processes by double-clicking the element in the **Call Stack** window, the **Threads** window, or the **Processes** window.
Copy file name to clipboardExpand all lines: docs/debugger/context-operator-cpp.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -55,21 +55,21 @@ You can use the context operator in C++ to qualify a breakpoint location, variab
55
55
56
56
For example, to set a breakpoint at the `SomeFunction` function of EXAMPLE.dll:
57
57
58
-
```cpp
58
+
```C++
59
59
{,,EXAMPLE.dll}SomeFunction
60
60
```
61
61
62
62
2.*module*!*expression*
63
63
64
-
```cpp
64
+
```C++
65
65
EXAMPLE.dll!SomeFunction
66
66
```
67
67
68
68
- *module* is the name of a module. You can use a full path to disambiguate between modules with the same name.
69
69
70
70
If the *module* path includes a comma, an embedded space, or a brace, you must use quotation marks around the path so that the context parser can properly recognize the string. Single quotation marks are considered part of a Windows file name, so you must use double quotation marks. For example,
0 commit comments