Skip to content

Commit 39a629e

Browse files
authored
Merge pull request #7345 from Mikejo5000/mikejo-dbg
Debugging additions for extended C# tutorial
2 parents f1d4765 + 3bc10c1 commit 39a629e

7 files changed

+104
-0
lines changed
Loading
Loading
Loading
Loading
Loading
Loading

docs/get-started/csharp/tutorial-console-part-2.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,110 @@ Real-world code involves many projects working together in a solution. Now, let'
323323
}
324324
```
325325

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**):
331+
332+
```csharp
333+
result = calculator.DoOperation(cleanNum1, cleanNum2, op);
334+
```
335+
336+
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+
![Screenshot of setting a breakpoint](media/vs-2019/calculator-2-debug-set-breakpoint.png)
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+
![Screenshot of hitting a breakpoint](media/vs-2019/calculator-2-debug-hit-breakpoint.png)
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+
![Screenshot of viewing a DataTip](media/vs-2019/calculator-2-debug-view-datatip.png)
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+
![Screenshot of Locals window](media/vs-2019/calculator-2-debug-locals-window.png)
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+
![Screenshot of step into command](media/vs-2019/calculator-2-debug-step-into.png)
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+
![Screenshot of the call stack](media/vs-2019/calculator-2-debug-call-stack.png)
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+
The Step Over command is similar to the Step Into command, except that if the current statement calls a function, the debugger runs the code in the called function and doesn't suspend execution until the function returns. Step Over is a faster way to navigate code if you're not interested in a particular function.
398+
399+
1. Press **F10** one more time so that the app pauses on the following line of code.
400+
401+
```csharp
402+
if (num2 != 0)
403+
{
404+
```
405+
406+
This code checks for a divide-by-zero case. If the app continues, it will throw a general exception (an error), but let'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.
407+
408+
## Debug: test a temporary change
409+
410+
1. Select the yellow pointer, currently paused on the `if (num2 != 0)` statement, and drag it to the following statement.
411+
412+
```csharp
413+
result = num1 / num2;
414+
```
415+
416+
By doing this, the app completely skips the `if` statement, so you can see what happens when you divide by zero.
417+
418+
1. Press **F10** to execute the line of code.
419+
420+
1. Hover over the `result` variable and you see it stores a value of `Infinity`.
421+
422+
In C#, `Infinity` is the result when you divide by zero.
423+
424+
1. Press **F5** (or, **Debug** > **Continue Debugging**).
425+
426+
The Infinity symbol shows up in the console as the result of the math operation.
427+
428+
1. Close the app properly by using the 'n' command.
429+
326430
## Next steps
327431

328432
Congratulations on completing this tutorial! To learn even more, continue with the following tutorials.

0 commit comments

Comments
 (0)