- 1. Introduction to Debugging & Breakpoints
- 2. Inserting a Breakpoint
- 3. Bring-Up Debugging Toolbar Commands
- 4. Step-Over Debug Option
- 5. Step-In and Step-Out Option
- 6. The Breakpoints Window
- 7. Conditional Breakpoint
- 8. Code Listings & Download
1. Introduction to Debugging & Breakpoints
Debugging is the process of finding the logical errors in the program by checking the code during the execution. The execution of a program pauses when the break-point is hit. Here, I will walk you through debugging the sample form supplied with this article. You can download it here: Debugging Basics Example or refer the complete code at the end of this Article.
The screen shot of the application is below:
The first text box will accept any number which is lesser than 36,001. Get Prime Sum button will calculate summation of all the Prime Numbers between 1 and number given in the input. The multi-line enabled text box shows the prime numbers. Label control below the multi-line text box shows the summation of the prime number.
In this example, we will not explore the code that runs behind this form. But we will explore some debugging techniques which we can use in our program to get away from the Logical Errors.
2. Inserting a Breakpoint
As already told, Breakpoint pauses the program to examine a certain piece of code. Let us first put a Break-Point on the event handler for the Get Prime Sum button. To do that, follow the Steps shown below:
- Open the downloaded solution.
- Locate the source code for the Get Prime Sum Handler.
- In the margin, left click. The below picture shows where we have to click:
Now, you can notice the change in the statement’s color and there is a red ball in the margin area where you clicked. We call this as Break-Point. When we run the program and when the execution reaches the Break-Point, the program pauses on the Break-Point. The below picture shows how a Breakpoint looks in the margin area of the code window:
Now, run the application and type 16 in the small text box. Finally, click the Get Prime Sum button. Notice how the program pauses the execution at the Break-Point. You will get a pointing arrow showing the current statement under examination. The picture below shows this:
Press F5 and close the running application.
3. Bring-Up Debugging Toolbar Commands
Stepping through the statements one by one allows us to examine the state of each variable and effect of the statement execution. We will see how the debugging commands work in this section. First, have a look at the screenshot below:
If you are not able to see this Toolbar try the below steps.
Right click next to any of the toolbar buttons already displayed. From the displayed menu select debug:
If you still do not see all the above-specified toolbar buttons, follow the Attempt 2.
- Click on the “Tools|Customize…” menu option
- From the displayed dialog under the “Command-Tab”, select the debug item in the category list box.
- Drag & Drop the commands to debugging toolbar as shown in the red box below. You can add these commands the same way to the menu as well.
4. Step-Over Debug Option
Each time you click Step-Over Button, the next statement will get executed. Now run the application again and type 16 in the input text box and I hope the Break-Point we placed in the previous section still exist on the same location. When you click the command button in the form, the program will hit the Breakpoint. Now, click on the “Step-Over”. Now C# debugger executes the statement txtOutput.text = “” and pointing arrow moves to next statement. Click the Step-Over Button till you reach the last statement as shown below:
In the current statement, you can place the cursor over “Text” portion. A pop-up will display in a moment and shows value present in the Text Property. In the below picture shows this in a red box stating the value in the text is 16:
One can examine a change in the variable value by stepping over the statements one by one. You can hit F5 button from the keyboard to run the application as usual.
5. Step-In and Step-Out Option
I once again expect that the Breakpoint is not yet removed. If you already removed it, place it back on the same statement and run the sample application with 12 in the input box. Once execution stops at the second statement inside the button click event handler, click the ‘Step-Over Button’. Now, we are at the ‘ if’ conditional statement. We need Step-In and Step-Out here in this statement to analyse it properly.
The return value of the function ‘ IsNumberValid’ is part of the condition. In the early part, when we did ‘Step-Over Debugging’, the execution gone to the next line. It means, the function is executed before moving to the next statement. Unlike Step-Over Debugging, the ‘Step-Into’ command passes the execution inside first line in the function. Now click the Step-Into Button and observe execution moving into the function as in the below:
Now, click on the Step-Over toolbar button repeatedly until you reach the conditional statement, if x <= 36000. Here, you can examine the content of the variable x.
While debugging, a Function Call or Constructor or Overloaded Operator encounters as part of the current statement, the Step-Into option will jump inside it. The Step-Out is a reverse of it. Say, for example, in our case, after stepping over three statements inside the function you may decide it not worth to debug rest of the statement. In that case, the Step-Out will help to jump out of the current function. This means, it still executes remaining statement and breaks at the calling function. All right, Now click the Step-Out button to go back to the statement that called the ‘ IsNumberValid’ function.
6. The Breakpoints Window
With a Breakpoint Window, one can easily manage multiple break points. The below screenshot shows a Break-Point Window:
Each Line item in the window represents a Break-Point. When you remove the check mark of an item in the window, C# disables the Break-Point which belongs to that item. You can see a disabled Breakpoint in the above screenshot’s code area. As an alternate method, you can right click the Breakpoint Bubble in the code area margin and disable it using the context menu.
The above picture shows location of a Breakpoint icon in the toolbar. You can bring the Break-Point Window by clicking it. If you cannot see the toolbar icon, bring it from the customize option which we already explored in section 3 of this article.
7. Conditional Breakpoint
7.1 The Need
After using the Breakpoint for some period on your big projects, you will come to a case where you end up like, “Hey, I need to break the execution of my loop when the loop is shooting for 160th time” or “I need to break my execution when a value of a specific variable got changed”. Of course, it is not a big deal as we can set up special piece of code which meets the need through a conditional check and put a Break-point inside the conditional body. But this compels a re-compilation. And, in some corporate project, this re-compilation and re-launch waste a considerable amount of time because of the volume code involved in it. The ‘Conditional Breakpoint’ overcomes this situation as it does not require code recompilation. Let us try the Conditional Break-Point with the downloaded application.
7.2 Setting-up a Conditional Breakpoint
First let we place a conditional Break-Point in our sample code. The Conditional Breakpoint evaluates a Boolean expression and then it breaks when the expression returns true. You can follow the below steps to do that:
- Locate the function SumIfPrime.
- Once you located the function put a Breakpoint on a statement shown in the below picture:
- Right click the Breakpoint and select Condition… from the context menu.
- In the Breakpoint Condition dialog, we have to place a tick mark on the Condition check box.
Enter the condition in the text box as shown in the below picture and then click on the OK Button after selecting the ‘Is True’ option. This will dismiss the dialog. Below is the dialog showing the condition for the Breakpoint:
Because of the ‘Is true’ option, the above Break-Point will be hit, only when the expression is evaluated to true. The expression will be true when the value of the variable ‘ i’ is between 20 to 30. Also, note the Breakpoint location which pauses the execution based on the supplied condition. Inside the For Loop, each time when the execution reaches the Breakpoint, C# evaluates the expression supplied as part of Conditional Breakpoint. It pauses the execution only when the expression returns true. In our case, the Conditional Breakpoint stops the execution for all the prime numbers that comes in between 20 and 30. Below is the screenshot that shows how the Breakpoint looks after setup:
7.3 Testing Our Conditional Breakpoint
To test this Conditional Breakpoint, first we must launch the application. Then, type 137 in the input box. At last, click the Get Prime Sum button. You can notice that program breaks at the Break-Point after testing the condition to true.
The ‘Has Changed’ option in the Conditional Breakpoint Window is for observing a specific variable and break the execution when it changes. You can try the below steps to know how ‘Has Changed’ option work:
- Right the click the Conditional Breakpoint.
- In the condition text box type the name of the variable, ‘ PrimeSum’
- Check the radio button option named ‘Has Changed’.
Now you can test the application. The Program breaks the execution at the Breakpoint when the value in the ‘ PrimeSum’ changes.
8. Code Listings & Download
Form Behind Code
public partial class frmDebug : Form
//001 : Declarations
int PrimeSum = 0;
//002: Say yes when the number entered is less than 36000+1
if (x <= 36000)
//003: Check the given number is prime or Not
bool IsPrime(int theNumber)
if (theNumber == 1 || theNumber == 2) return true;
for (int i = 2; i < theNumber; i++)
if ((theNumber % i) == 0)
//004: When a passed in number is prime, sum it.
void SumIfPrime(int theNumber)
for (int i = 1; i <= theNumber; i++)
if (IsPrime(i) == true)
txtOutput.AppendText(i.ToString() + ", ");
PrimeSum = PrimeSum + i;
//005: Initiate the Prime sum Calculation
private void btnGetResult_Click(object sender,
txtOutput.Text = "";
if (IsNumberValid() == true &&
int.TryParse(txtInput.Text, out x) == true)
txtOutput.Text = txtOutput.Text.Remove(
txtOutput.Text.Length - 2);
lblResult.Text = "Prime Number Sum = "
Source Code: Download from Google Drive