Share via


How to: Debug Your Visual C# Express Application

This topic discusses the basics about how to use the Visual C# Express Edition debugger. In this topic, you learn by doing. You will not be looking for an actual bug, but you will be using the debugger to obtain information about a simple application that you create.

Procedures

To practice debugging an application

  1. Start Visual C# Express Edition.

  2. On the File menu, click New Project.

    The New Project dialog box appears.

  3. In the New Project dialog box, click Windows Forms Application, and then click OK.

    A new Windows Forms project opens and displays a new form in the Windows Forms Designer.

  4. From the Toolbox, drag a TextBox control to the form.

  5. Drag a Button from the Toolbox to the form and position it next to the TextBox.

  6. Double-click the button to create the default Click event handler and display the Code Editor.

  7. Add the following code the button1_Click event handler.

    textBox1.Text = "Button was clicked!";
    
  8. On the Build menu, click Build Solution.

    The project builds without errors.

  9. In the Code Editor, click the left margin on the same line as the text you added.

    A red dot appears in the margin, and the line of code is highlighted. This is known as adding a breakpoint. The breakpoint will temporarily stop execution of the program right before that line of code is run. The following illustration shows a breakpoint in the IDE.

    Setting a Breakpoint

    Break Point

  10. On the Debug menu, click Start Debugging.

    The Windows Form starts to run.

  11. Click the button and confirm that the code execution has stopped at the line of code where you added the breakpoint and that the code is highlighted in yellow.

  12. On the Debug menu, point to Windows, and then click Watch.

  13. In the Watch window, click in the first row under the Name heading, type textBox1.Text, and then press ENTER.

    The Watch window shows the value of this variable in quotation marks as shown in the following illustration:

    Watch window at breakpoint

    Watch Window at Break Point

  14. On the Debug menu, click Step Into.

    The value of textBox1.Text changes in the Watch window to "Button was clicked!", as shown in the following illustration:

    Watch window

    Watch Window

    Note

    Step Into enables you to move through your code one line at a time. The command is called Step Into because if the next statement is a method call, the debugger will step into it and execute the first line of the called method instead of the next line of the current method.

  15. On the Debug menu, click Continue.

    The application runs and displays the text in the text box. Clicking Continue during a debugging session is like clicking Start to begin a debugging session: it prompts uninterrupted execution until a breakpoint is encountered.

  16. The application should stop execution on its own. If it doesn't, on the Debug menu, click Break All, or press Ctrl+Alt+Break, and then click Stop Debugging.

    To learn more about how to use the debugger, see the Debugger Roadmap.

See Also

Concepts

C# Language Primer

Other Resources

Using the Visual C# Express IDE

Visual C# Express Tips and Tricks