There is also a very good tutorial on DIC here which complements this one. That one is an excellent walk-through of the debugging process, and describes the thought-processes necessary to correctly debug your application. My version is more of an introduction to, and overview of, the Visual Studio features that you can use to debug. I recommend reading both!
The shortcut-keys are for the VS Express version, followed by the VS (full), or alternative, shortcut.
Here is the C# Console Application code that I will be using.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
int[] numbers = { 10, 20, 45, 50, 80 };
int total = 0;
for (int i = 1; i <= 5; i++) {
total += numbers[i];
}
Console.WriteLine("The total is {0:D}", total);
Console.ReadKey();
}
}
}
If I run this I receive the following error message:

If you press Break at this point you are already beginning the debugging process!
[Spoiler: The solution is for (int i = 0; i <= 4; i++) { because array-indexing starts at zero.]
When debugging you can step-through your code one line at a time, observe what happens to your variables, jump to specific lines, etc., etc. To START THE PROCESS you can:
1. Choose the Debug menu and Step Into (or press the F11 or F8 function key).

Continue to press F11, stepping through your code one line at a time.
WHAT YOU CAN DO WHILE STEPPING THROUGH
Point your mouse over any variable (and other values) and a pop-up will display its current value. If it is, for example, an array then you can click on the plus sign that appears within the pop-up to expand it, showing more details that you can navigate through.

This works for object-references, etc., as well, and you can navigate through entire structures!
As you are stepping through there are two other options on the Debug menu: Step Over (F10 or Shift-F8) and Step Out (Shift-F11 or Ctrl-Shift-F8).

Step Over: If you are about to call another function or method, just run through it without pausing, returning to the current code block.
Step Out: If your code has called (moved into) another function or method then complete this without pause, returning to the previous (calling) code-block.
You can also drag the yellow arrow on the left up or down to skip, or re-execute, lines;
You can right-click a line and choose Run to Cursor (Ctrl-F10).
You can abandon debugging at any point by pressing the red square (Stop Debugging) (Shift-F5 or Ctrl-Alt-Break). Or press the Start button, which now says Continue (F5) to run the rest of your code. (Generally it is better to let the code run to completion, particularly for more complex applications, so that all object-references are correctly disposed of.)
***************
2. Click to the far left of a statement to create a Breakpoint (F9) - repeat to remove the breakpoint (this is available from the Debug menu as well). Press Start and the code will run until it reaches the breakpoint and then pause for you to step through.

You can set a number of Breakpoints - Ctrl-Shift-F9 removes all breakpoints.
Refer back to point 1. for details of what you can do while stepping through.
3. Right-click an executable line (not a comment) and choose Run to Cursor (Ctrl-F10) - my favourite method! The code will run all the way until it reaches this line (assuming, of course, that your code will actually encounter this line).

Refer back to point 1. for details of what you can do while stepping through.
Locals Window
The Locals Windows will display the values of all (currently in scope) variables, including arrays, lists, etc. Once you have started debugging choose the Debug menu, Windows, Locals (Alt-4).

Note The Debug, Windows menu option only shows a limited number of windows until you actually start debugging - then many other windows become available to you. Investigate the Immediate, Watch and Call Stack Windows.
The Locals window appears as a separate tabbed panel (usually to the bottom-left of the screen) alongside the Output and other windows.

MSDN
I've linked to the VS 2010 version, rather than 2012, to avoid the distraction of any Metro features.




MultiQuote




|