Visual C# 2010 Debugger

profileActiveNow
problem_1.docx

G

We are built to make mistakes,

coded for error.

—Lewis Thomas

Using the Visual C# 2010

Debugger

What we anticipate seldom

occurs; what we least expect

generally happens.

—Benjamin Disraeli

It is one thing to show a man

that he is in error, and another

to put him in possession of

truth.

—John Locke

He can run but he can’t hide.

—Joe Louis

Objectives

In this appendix you will learn:

To use breakpoints to pause

program execution and allow

you to examine the values of

variables.

To set, disable and remove

breakpoints.

To use the Continue

command to continue

execution from a breakpoint.

To use the Locals window to

view/modify variable values.

To use the Watch window to

evaluate expressions.

To use the Step Into, Step

Out and Step Over

commands to execute a

program line by line.

To use Just My Code™

debugging.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G.1 Introduction

G.1 Introduction

G.2 Breakpoints and the Continue

Command

G.3 DataTips and Visualizers

G.4 The Locals and Watch Windows

G.5 Controlling Execution Using the

Step Into, Step Over, Step Out

and Continue Commands

G.6 Other Debugging Features

G.6.1 Exception Assistant

G.6.2 Just My Code™ Debugging

G.6.3 Other Debugger Features

G-2

G.1 Introduction

In this appendix, you’ll learn about tools and techniques that can be used to address com-

pilation errors and logic errors. Syntax errors are a type of compilation error—an error

that prevents code from compiling. Logic errors, also called bugs, do not prevent a pro-

gram from compiling successfully, but can cause a running program to produce erroneous

results or terminate prematurely. Most compiler vendors, like Microsoft, package their

IDEs with a tool called a debugger. Debuggers allow you to monitor the execution of your

programs to locate and remove logic errors. A program must successfully compile before

it can be used in the debugger. The debugger allows you to suspend program execution,

examine and set variable values and much more. In this appendix, we introduce the Visual

C# 2010 IDE and debugger features for fixing errors in your programs.

G.2 Breakpoints and the Continue Command

While compilation errors can be found automatically by the compiler, it can be much

more difficult to determine the cause of logic errors. To help with this, we investigate the

concept of breakpoints. Breakpoints are special markers that can be set at any executable

line of code. They cannot be placed on comments or whitespace. When a running pro-

gram reaches a breakpoint, execution pauses, allowing you to examine the values of vari-

ables to help determine whether logic errors exist. For example, you can examine the value

of a variable that stores a calculation’s result to determine whether the calculation was per-

formed correctly. You can also examine the value of an expression.

To illustrate the debugger features, we use the program in Figs. G.1–G.2 that creates

and manipulates an Account (Fig. G.1) object. This example is based on concepts from

Chapter 4, so it does not use features that are presented after Chapter 4. Execution begins

in Main (lines 8–41 of Fig. G.2). Line 10 creates an Account object with an initial balance

of $50.00. Account’s constructor (lines 10–13 of Fig. G.1) accepts one argument, which

specifies the Account’s initial balance. Lines 13–14 of Fig. G.2 output the initial account

balance using Account property Balance. Lines 18–20 prompt the user for and input the

withdrawalAmount. Lines 22–24 subtract the withdrawal amount from the Account’s

balance using its Debit method. Line 27 displays the new balance. Next, lines 30–40

perform similar steps to credit the account.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G.2 Breakpoints and the Continue Command

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

G-3

// Fig. G.1: Account.cs

// Account class with a Debit method that withdraws money from account.

using System;

public class Account

{

private decimal balance; // instance variable that stores the balance

// constructor

public Account( decimal initialBalance )

{

Balance = initialBalance; // set balance using property

} // end Account constructor

// credits (adds) an amount to the account

public void Credit( decimal amount )

{

Balance = Balance + amount; // add amount to balance

} // end method Credit

// debit (subtracts) an amount from the account

public void Debit( decimal amount )

{

if ( amount > Balance )

Console.WriteLine( "Debit amount exceeded account balance." );

if ( amount <= Balance )

Balance = Balance - amount; // subtract amount from balance

} // end method Debit

// property to get the balance

public decimal Balance

{

get

{

return balance;

} // end get

set

{

// validate that value is greater than or equal to 0;

// if it is not, balance is left unchanged

if ( value >= 0 )

balance = value;

} // end set

} // end property Balance

} // end class Account

Account

Fig. G.1 |

1

2

3

class with a Debit method that withdraws money from account.

// Fig. G.2: AccountTest.cs

// Creating and manipulating an Account object.

using System;

Fig. G.2 | Creating and manipulating an Account object. (Part 1 of 2.)

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G-4

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

Appendix G

Using the Visual C# 2010 Debugger

public class AccountTest

{

// Main method begins execution of C# application

public static void Main( string[] args )

{

Account account1 = new Account( 50.00M ); // create Account object

// display initial balance of account object

Console.WriteLine( "account1 balance: {0:C}",

account1.Balance );

decimal withdrawalAmount; // withdrawal amount entered by user

Console.Write( "Enter withdrawal amount for account1: " );

// obtain user input

withdrawalAmount = Convert.ToDecimal( Console.ReadLine() );

Console.WriteLine( "\nsubtracting {0:C} from account1 balance",

withdrawalAmount );

account1.Debit( withdrawalAmount ); // subtract amount from account1

// display balance

Console.WriteLine( "account1 balance: {0:C}", account1.Balance );

Console.WriteLine();

Console.Write( "Enter credit amount for account1: " );

// obtain user input

decimal creditAmount = Convert.ToDecimal( Console.ReadLine() );

Console.WriteLine( "\nadding {0:C} to account1 balance",

creditAmount );

account1.Credit( creditAmount );

// display balance

Console.WriteLine( "account1 balance: {0:C}", account1.Balance );

Console.WriteLine();

} // end Main

} // end AccountTest

account1 balance: $50.00

Enter withdrawal amount for account1: 25

subtracting $25.00 from account1 balance

account1 balance: $25.00

Enter credit amount for account1: 33

adding $33.00 to account1 balance

account1 balance: $58.00

Fig. G.2 | Creating and manipulating an Account object. (Part 2 of 2.)

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G.2 Breakpoints and the Continue Command

G-5

In the following steps, you’ll use breakpoints and debugger commands to examine

variable withdrawalAmount’s value (declared in Fig. G.2) while the program executes.

1. Inserting breakpoints in Visual C#. First, ensure that AccountTest.cs is open in

the IDE’s code editor. To insert a breakpoint, left click inside the margin indicator

bar (the gray margin at the left of the code window in Fig. G.3) next to the line of

code at which you wish to break, or right click that line of code and select

Breakpoint > Insert Breakpoint. Additionally, you can also press F9 when your cur-

sor is on the line to toggle the breakpoint. You may set as many breakpoints as you

like. Set breakpoints at lines 18, 24 and 41 of your code. [Note: If you have not al-

ready done so, have the code editor display line numbers by opening

Tools > Options…, navigating to Text Editor > C# and selecting the Line numbers

checkbox.] A solid circle appears in the margin indicator bar where you clicked, and

the entire code statement is highlighted, indicating that breakpoints have been set

(Fig. G.3). When the program runs, the debugger suspends execution at any line

that contains a breakpoint. The program then enters break mode. Breakpoints can

be set before running a program, both in break mode and during execution. To

show a list of all breakpoints in a project, select Debug > Windows > Breakpoints.

This feature is available only in the full version of Visual Studio 2010.

Margin indicator bar

Breakpoints

Fig. G.3 | Setting breakpoints.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G-6

Appendix G

Using the Visual C# 2010 Debugger

2. Beginning the debugging process. After setting breakpoints in the code editor, se-

lect Build > Build Solution to compile the program, then select Debug > Start

Debugging (or press the F5 key) to begin the debugging process. While debugging

a console application, the Command Prompt window appears (Fig. G.4), allowing

program interaction (input and output).

Fig. G.4 |

Account program running.

3. Examining program execution. Program execution pauses at the first breakpoint

(line 18), and the IDE becomes the active window (Fig. G.5). The yellow arrow

to the left of line 18, also called the Instruction Pointer, indicates that this line

contains the next statement to execute. The IDE also highlights the line as well.

Instruction Pointer

Next executable statement

Fig. G.5 | Program execution suspended at the first breakpoint.

4. Using the

command to resume execution. To resume execution, select

Debug > Continue (or press the F5 key). The Continue command executes the

statements from the current point in the program to the next breakpoint or the

end of Main, whichever comes first. It is also possible to drag the Instruction Point-

er to another line in the same method to resume execution starting at that posi-

tion. Here, we use the Continue command, and the program continues executing

and pauses for input at line 20. Enter 25 in the Command Prompt window as the

withdrawal amount. When you press Enter, the program executes until it stops at

the next breakpoint (line 24). Notice that when you place the mouse pointer over

the variable name withdrawalAmount, its value is displayed in a Quick Info box

(Fig. G.6). As you’ll see, this can help you spot logic errors in your programs.

Continue

5. Continuing program execution. Use the Debug > Continue command to execute

line 24. The program then asks you to input a credit (deposit) amount. Enter 33,

then press Enter. The program displays the result of its calculation (Fig. G.7).

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G.2 Breakpoints and the Continue Command

G-7

Fig. G.6 | Quick Info box displays value of variable withdrawalAmount.

Fig. G.7 | Sample execution of Account.exe in debug mode.

6. Disabling a breakpoint. To disable a breakpoint, right click a line of code in which

the breakpoint has been set and select Breakpoint > Disable Breakpoint. The dis-

abled breakpoint is indicated by a hollow circle (Fig. G.8)—the breakpoint can be

reenabled by right clicking the line marked by the hollow circle and selecting

Breakpoint > Enable Breakpoint.

Disabled breakpoint

Fig. G.8 | Disabled breakpoint.

7. Removing a breakpoint. To remove a breakpoint that you no longer need, right

click the line of code on which the breakpoint has been set and select

Breakpoint > Delete Breakpoint. You also can remove a breakpoint by clicking the

circle in the margin indicator bar or pressing F9 when the cursor is on the line.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G-8

Appendix G

Using the Visual C# 2010 Debugger

8. Finishing program execution. Select Debug > Continue to execute the program to

completion. Then delete all the breakpoints.

G.3 DataTips and Visualizers

You already know how to use the Quick Info window to view a variable’s value. However,

often you may want to check the status of an object. For example, you may want to check

the Text value of a TextBox control. When you hover the mouse over a reference-type vari-

able while debugging, the DataTip window appears (Fig. G.9). When you hover over the

+ sign in the DataTip, the DataTip window gives information about the object’s data.

There are some limitations—references must be instance variables or local variables, and

expressions involving method calls cannot be evaluated.

DataTip window

Fig. G.9 | A DataTip displayed for the account1 variable.

For the Account object, this means that you can see the balance inside it (as well as

the Balance property used to access it). Just like the Quick Info window, you can also

change the value of a property or variable inside it by clicking on one of the values listed,

then typing the new value.

DataTips do not intuitively display information for all variables. For example, a vari-

able representing an XML document cannot be viewed in its natural form using most

debugging tools. For such types, visualizers can be useful. Visualizers are specialized

windows to view certain types of data. They are shown through DataTip windows by

clicking the small magnifying glass next to a variable name. There are three predefined

visualizers—advanced programmers may create additional ones. The Text Visualizer lets

you see string values with all their formatting included. The XML Visualizer formats

XML objects into a color-coded format. Finally, the HTML Visualizer parses HTML code

(in string or XML form) into a web page, which is displayed in the small window.

G.4 The Locals and Watch Windows

In the preceding section, you learned how to use the Quick Info and DataTip features to

examine the variable’s value. In this section, you’ll learn how to use the Locals window to

view all variables that are in use while your program is running. You’ll also use the Watch

window to examine the values of expressions.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G.4 The Locals and Watch Windows

G-9

1. Inserting breakpoints. Set a breakpoint at line 24 (Fig. G.10) in the source code

by left clicking in the margin indicator bar to the left of line 24. Use the same

technique to set breakpoints at lines 27 and 28 as well.

Fig. G.10 | Setting breakpoints at lines 24, 27 and 28.

2. Starting debugging. Select Debug > Start Debugging. Type 25 at the Enter with-

drawal amount for account1: prompt (Fig. G.11) and press Enter. The program

executes until the breakpoint at line 24.

Fig. G.11 | Entering the withdrawal amount before the breakpoint is reached.

3. Suspending program execution. When the program reaches line 24, the IDE sus-

pends program execution and switches the program into break mode (Fig. G.12).

At this point, the statement in line 20 (Fig. G.2) has input the withdrawal-

Amount that you entered (25), the statement in lines 22–23 has output that the

program is subtracting that amount from the account1 balance and the state-

ment in line 24 is the next statement that executes.

4. Examining data. Once the program enters break mode, you can explore the local

variable values using the Locals window. If this window is not displayed, select

Debug > Windows > Locals. Click the plus to the left of account1 in the Locals win-

dow’s Name column (Fig. G.13). This allows you to view each of account1’s in-

stance variable values individually, including the value for balance (50). The Locals

window displays a class’ properties as data, which is why you see both the Balance

property and the balance instance variable in the window. In addition, the current

value of local variable withdrawalAmount (25) is displayed.

5. Evaluating arithmetic and boolean expressions. You can evaluate arithmetic and

bool expressions using the Watch window. Select Debug > Windows > Watch to

display the window (Fig. G.14). In the Name column’s first row (which should

be blank initially), type (withdrawalAmount + 10) * 5, then press Enter. The val-

ue 175 is displayed (Fig. G.14). In the Name column’s next row in the Watch

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G-10

Appendix G

Using the Visual C# 2010 Debugger

Fig. G.12 | Program execution pauses when debugger reaches the breakpoint at line 24.

Fig. G.13 | Examining local variables.

window, type withdrawalAmount == 200, then press Enter. This expression deter-

mines whether the value contained in withdrawalAmount is 200. Expressions con-

taining the == symbol are boolean expressions. The value returned is false

(Fig. G.14), because withdrawalAmount does not currently contain the value 200.

6. Resuming execution. Select Debug > Continue to resume execution. Line 24 exe-

cutes, subtracting the account with the withdrawal amount, and the program enters

break mode again at line 27. Select Debug > Windows > Locals. The updated bal-

ance instance variable and Balance property value are now displayed (Fig. G.15).

The values in red in the window are those that have just been modified.

Evaluating an

arithmetic

expression

Evaluating a

bool

expression

Fig. G.14 | Examining the values of expressions.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G.5 Step Into, Step Over, Step Out and Continue Commands

G-11

Updated value of

the balance

variable appears in

red on the screen

Fig. G.15 | Displaying the value of local variables.

7. Modifying values. Based on the value input by the user (25), the account balance

output by the program should be 25. However, you can use the Locals window

to change variable values during program execution. This can be valuable for ex-

perimenting with different values and for locating logic errors in programs. In the

Locals window, click the Value field in the balance row to select the value 25.

Type 37, then press Enter. The debugger changes the value of balance (and the

Balance property as well), then displays its new value in red (Fig. G.16). Now

select Debug > Continue to execute lines 27–28. Notice that the new value of bal-

ance is displayed in the Command Prompt window.

Value modified in

the debugger

Fig. G.16 | Modifying the value of a variable.

8. Stopping the debugging session. Select Debug > Stop Debugging. Delete all break-

points, which can be done by pressing Shift + F5.

Sometimes you need to execute a program line by line to find and fix logic errors. Stepping

through a portion of your program this way can help you verify that a method’s code ex-

ecutes correctly. The commands you learn in this section allow you to execute a method

line by line, execute all of a method’s statements or execute only its remaining statements

(if you have already executed some statements in the method).

1. Setting a breakpoint. Set a breakpoint at line 24 by left clicking in the margin

indicator bar.

2. Starting the debugger. Select Debug > Start Debugging. Enter the value 25 at the

Enter withdrawal amount for account1: prompt. Program execution halts when the

program reaches the breakpoint at line 24.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G.5 Controlling Execution Using the Step Into, Step

Over, Step Out and Continue Commands

G-12

Appendix G

Using the Visual C# 2010 Debugger

3. Using the Step Into command. The Step Into command executes the next statement

in the program and immediately halts. If the statement to execute is a method call,

control transfers to the called method. The Step Into command allows you to follow

execution into a method and confirm its execution by individually executing each

statement inside the method. Select Debug > Step Into (or press F11) to enter class

Account’s Debit method (Fig. G.17).

Fig. G.17 | Stepping into the Debit method.

4. Using the Step Over command. Select Debug > Step Over (or press F10) to enter

the Debit method’s body and transfer control to line 25. The Step Over com-

mand behaves like the Step Into command when the next statement to execute

does not contain a method call or access a property. You’ll see how the Step Over

command differs from the Step Into command in Step 10.

5. Using the Step Out command. Select Debug > Step Out or press Shift-F11 to exe-

cute the remaining statements in the method and return control to the calling

method. Often, in lengthy methods, you may want to look at a few key lines of

code, then continue debugging the caller’s code. The Step Out command exe-

cutes the remainder of a method and returns to the caller.

6. Setting a breakpoint. Set a breakpoint at line 28 of Fig. G.2. This breakpoint is

used in the next step.

7. Using the Continue command. Select Debug > Continue to execute until the next

breakpoint is reached at line 20. This feature saves time when you do not want to

step line by line through many lines of code to reach the next breakpoint.

8. Stopping the debugger. Select Debug > Stop Debugging to stop debugging.

9. Starting the debugger. Before we can demonstrate the next debugger feature, you

must restart the debugger. Start it, as you did in Step 2, and enter the same value

(25). The debugger pauses execution at line 24.

10. Using the Step Over command. Select Debug > Step Over (Fig. G.18). Recall that

this command behaves like the Step Into command when the next statement to

execute does not contain a method call. If the next statement to execute contains

a method call, the called method executes in its entirety (without pausing execu-

tion at any statement inside the method—unless there is a breakpoint in the

method), and the arrow advances to the next executable line (after the method

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G.6 Other Debugging Features

G-13

call) in the current method. In this case, the debugger executes line 24 in Main

(Fig. G.2), which calls the Debit method. Then the debugger pauses execution

at line 27, the next executable statement.

The Debit method executes without stepping into it

when you select the Step Over command

Fig. G.18 | Using the debugger’s Step Over command.

11. Stopping the debugger. Select Debug > Stop Debugging. Remove all remaining

breakpoints.

G.6 Other Debugging Features

Visual C# 2010 provides many other debugging features that simplify the testing and de-

bugging process. We discuss some of these features in this section.

G.6.1 Exception Assistant

You can run a program by selecting either Debug > Start Debugging or Debug > Start With-

out Debugging. If you select the option Debug > Start Debugging and the runtime environ-

ment detects uncaught exceptions, the application pauses, and a window called the

Exception Assistant appears, indicating where the exception occurred, the exception type

and links to helpful information on handling the exception. We discuss the Exception As-

sistant in detail in Section 13.3.3.

G.6.2 Just My Code™ Debugging

Throughout this book, we produce increasingly substantial programs that often include a

combination of code written by the programmer and code generated by Visual Studio.

The IDE-generated code can be difficult to understand—fortunately, you rarely need to

look at this code. Visual Studio 2010 provides a debugging feature called Just My Code™

that allows programmers to test and debug only the portion of the code they have written.

When this option is enabled, the debugger always steps over method calls to methods of

classes that you did not write.

To enable this option, in the Options dialog, select the Debugging category to view the

available debugging tools and options. Then click the checkbox that appears next to the

Enable Just My Code (Managed only) option (Fig. G.19) to enable or disable this feature.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

G-14

Appendix G

Using the Visual C# 2010 Debugger

Fig. G.19 | Enabling the Just My Code debugging feature in Visual C#.

G.6.3 Other Debugger Features

You can learn more about the Visual Studio 2010 debugger at

msdn.microsoft.com/en-us/library/sc65sadd.aspx

G.7 Wrap-Up

In this appendix, you learned how to use the debugger and set breakpoints so that you can

examine your code and results while a program executes. This capability enables you to

locate and fix logic errors in your programs. You also learned how to continue execution

after a program suspends execution at a breakpoint and how to disable and remove break-

points.

We used DataTips to find additional information on nonprimitive variables, and Visu-

alizers to view certain data types in a detailed view. We showed how to use the debugger’s

Watch and Locals windows to evaluate arithmetic and boolean expressions. We also dem-

onstrated how to modify a variable’s value during program execution so that you can see

how changes in values affect your results.

You learned how to use the debugger’s Step Into command to debug methods called

during your program’s execution. You saw how the Step Over command can be used to

execute a method call without stopping the called method. You used the Step Out com-

mand to continue execution until the current method’s end. You also learned that the

Continue command continues execution until another breakpoint is found or the program

terminates.

Finally, we discussed the Visual C# 2010 debugger’s additional features, including

Edit and Continue, the Exception Assistant and Just My Code™ debugging.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Summary

G-15

Summary

Section G.1 Introduction

• Most compiler vendors, like Microsoft, provide software called a debugger, which allows you to

monitor the execution of your programs to locate and remove logic errors.

• The debugger allows you to suspend program execution, examine and set variables and much

more.

Section H.2 Breakpoints and the Continue Command

• Breakpoints are markers that can be set at any executable line of code. When program execution

reaches a breakpoint, execution pauses, allowing you to examine the values of variables to help

you locate and correct logic errors.

• To insert a breakpoint, left click the margin indicator bar next to the line of code at which you

wish to break, or right click that line of code and select Breakpoint > Insert Breakpoint.

• A program is said to be in break mode when the debugger pauses the program’s execution. Break-

points can be set before running a program, in break mode or while a program is running.

• The Continue command will execute any statements between the next executable statement and the

next breakpoint or the end of Main, whichever comes first.

• It is possible to drag the Instruction Pointer to another line in the same method to resume execu-

tion starting at that position.

• The Run To Cursor command will execute any statements between the next executable statement

and the line where your cursor currently is.

• The value that the variable stores is displayed in a Quick Info box. In a sense, you are peeking

inside the computer at the value of one of your variables.

• To disable a breakpoint, right click a line of code in which the breakpoint has been set and select

Breakpoint > Disable Breakpoint.

• To remove a breakpoint that you no longer need, right click the line of code in which the break-

point has been set and select Breakpoint > Delete Breakpoint. You also can remove a breakpoint by

clicking the solid circle in the margin indicator bar.

• When you hover the mouse over a reference-type variable, the DataTip window appears. When

you click the + sign, the DataTip window gives information about the object’s data.

• The DataTip window has some limitations—references must be instance variables or local vari-

ables, and expressions involving method calls cannot be evaluated.

• Just as with the Quick Info window, you can also change the value of a property or variable inside

an object by clicking one of the values listed, then typing the new value.

• Visualizers are specialized windows to view certain types of data. They’re shown through DataTip

windows by clicking the small magnifying glass next to a variable name.

• The Text Visualizer lets you see string values with all their formatting included.

• The XML Visualizer formats XML objects into a color-coded format.

• The HTML Visualizer parses HTML code (in string or XML form) into a web page, which is dis-

played in a small window.

• The Locals window enables you to assign new values to variables while your program is running.

• The Watch window allows you to examine the value of arithmetic and Boolean expressions.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Section G.3 DataTips and Visualizers

Section H.4 The Locals and Watch Windows

G-16

Appendix G

Using the Visual C# 2010 Debugger

Section G.5 Controlling Execution Using the Step Into, Step Over, Step Out and Con-

tinue Commands

• You can step through a portion of your program line by line to find and fix logic errors and verify

that a method’s code executes correctly.

• The Step Into command executes the next statement in the program and immediately halts. If

the statement to be executed as a result of the Step Into command is a method call, control is

transferred to the called method. The Step Into command allows you to enter a method and con-

firm its execution by individually executing each statement in the method.

• The Step Over command behaves like the Step Into command when the next statement to execute

does not contain a method call or access a property. If it does contain a method call or access a

property, the application executes the statement and advances to the next line.

• The Step Out command is used for situations where you do not want to continue stepping

through the entire method line by line.

Section G.6 Other Debugging Features

• The Exception Assistant indicates where the exception occurred, the type of the exception and

links to helpful information on handling the exception.

• The Just My Code™ feature allows programmers to test and debug only the portion of code

which they have written.

Terminology

break mode

breakpoint

bug

compilation error

Continue command

DataTip

debugger

disable a breakpoint

Exception Assistant

HTML Visualizer

margin indicator bar

Quick Info box

Run To Cursor command

Smart Compile Auto Correction

solid breakpoint circle

Step Into command

Step Out command

Step Over command

suspend program execution

Text Visualizer

Visualizer

insert a breakpoint

Instruction Pointer

Just My Code™ debugging

Locals window

logic error

Visual C# debugger

Watch window

XML Visualizer

Self-Review Exercises

G.1

Fill in the blanks in each of the following statements:

a) When the debugger suspends program execution at a breakpoint, the program is said to

be inmode.

feature in Visual C# .NET allows you to “peek into the computer”b) The

and look at the value of a variable.

c) You can examine the value of an expression by using the debugger’swin-

dow.

command behaves like the Step Into command when the next state-d) The

ment to execute does not contain a method call or access a property.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Answers to Self-Review Exercises

G.2

G-17

State whether each of the following is true or false. If false, explain why.

a) When program execution suspends at a breakpoint, the next statement to be executed

is the statement after the breakpoint.

b) When a variable’s value is changed, the value changes to yellow in the Locals windows.

c) During debugging, the Step Out command executes the remaining statements in the

current method and returns program control to the place where the method was called.

Answers to Self-Review Exercises

G.1

a) break. b) Quick Info box. c) Watch. d) Step Over.

G.2a) False. When program execution suspends at a breakpoint, the next statement to be exe-

cuted is the statement at the breakpoint. b) False. A variable’s value turns red when it is changed.

c) True.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

© 2011 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.