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/get-started/csharp/tutorial-console-part-2.md
+104Lines changed: 104 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -323,6 +323,110 @@ Real-world code involves many projects working together in a solution. Now, let'
323
323
}
324
324
```
325
325
326
+
## Debug: set and hit a breakpoint
327
+
328
+
The Visual Studio debugger is a powerful tool that allows you to run your code step by step, to find the exact point where you made a programming mistake. You then understand what corrections you need to make in your code. Visual Studio allows you to make temporary changes so you can continue running the program.
329
+
330
+
1. In *Program.cs*, click the margin to the left of the following code (or, open the shortcut menu and choose **Breakpoint** > **Insert Breakpoint**, or press **F9**):
The red circle that appears indicates a breakpoint. You can use breakpoints to pause your app and inspect code. You can set a breakpoint on any executable line of code.
337
+
338
+

339
+
340
+
1. Build and run the app.
341
+
342
+
1. In the running app, type some values for the calculation:
343
+
344
+
- For the first number, type **8** and enter it.
345
+
- For the second number, type **0** and enter it.
346
+
- For the operator, let's have some fun; type **d** and enter it.
347
+
348
+
The app suspends where you created the breakpoint, which is indicated by the yellow pointer on the left and the highlighted code. The highlighted code has not yet executed.
349
+
350
+

351
+
352
+
Now, with the app suspended you can inspect your application state.
353
+
354
+
## Debug: view variables
355
+
356
+
1. In the highlighted code, hover over variables such as `cleanNum1` and `op`. You see the current values for these variables (`8` and `d`, respectively), which appear in DataTips.
357
+
358
+

359
+
360
+
When debugging, checking to see whether variables hold the values you expect them to hold is often critical to fixing issues.
361
+
362
+
2. In the lower pane, look at the **Locals** window. (If it's closed, choose **Debug** > **Windows** > **Locals** to open it.)
363
+
364
+
In the Locals window, you see each variable that is currently in scope, along with its value and type.
365
+
366
+

367
+
368
+
3. Look at the **Autos** window.
369
+
370
+
The Autos window is similar to the **Locals** window, but it shows the variables immediately preceding and following the current line of code where your app is paused.
371
+
372
+
Next, you will execute code in the debugger one statement at a time, which is called *stepping*.
373
+
374
+
## Debug: step through code
375
+
376
+
1. Press **F11** (or **Debug** > **Step Into**).
377
+
378
+
Using the Step Into command, the app executes the current statement and advances to the next executable statement (usually the next line of code). The yellow pointer on the left always indicates the current statement.
379
+
380
+

381
+
382
+
You've just stepped into the `DoOperation` method in the `Calculator` class.
383
+
384
+
1. To get a hierarchical look at your program flow, look at the **Call Stack** window. (If it's closed, choose **Debug** > **Windows** > **Call Stack**.)
385
+
386
+

387
+
388
+
This view shows the current `Calculator.DoOperation` method, indicated by the yellow pointer, and the second row shows the function that called it, from the `Main` method in *Program.cs*. The **Call Stack** window shows the order in which methods and functions are getting called. In addition, it provides access to many debugger features, such as **Go to Source Code**, from the shortcut menu.
389
+
390
+
1. Press **F10** (or **Debug** > **Step Over**) several times until the app pauses on the `switch` statement.
391
+
392
+
```csharp
393
+
switch (op)
394
+
{
395
+
```
396
+
397
+
TheStepOvercommandissimilartotheStepIntocommand, exceptthatifthecurrentstatementcallsafunction, thedebuggerrunsthecodeinthecalledfunctionanddoesn't suspend execution until the function returns. Step Over is a faster way to navigate code if you'renotinterestedinaparticularfunction.
Thiscodechecksforadivide-by-zerocase. Iftheappcontinues, itwillthrowageneralexception (anerror), butlet's say you consider this a bug, and want to do something else, like view the actual returned value in the console. One option is to use a debugger feature called Edit-and-continue to make changes to the code and then continue debugging. However, we will show you a different trick to temporarily modify the execution flow.
0 commit comments