This post shows you how to use Breakpoints in Visual Studio with a hit count. Breakpoints can be described as bread and butter of debugging. They allow you to stop execution at any statement in your code. By doing this you can examine values in your variables, look at call stack, change values in variables and much more. 

Let’s follow this post with an example of a simple program which uses a loop to add a number to a List<int>.

static void Main(string[] args)
{
  List<int> numbers = new List<int>();
  for (int i = 0; i < 100; i++)
  {
    numbers.Add(i);
  }
}
 

We will now add a breakpoint to numbers.Add(i) line. A breakpoint can be added by clicking on gray area on the left side of code editor or by pressing F9.

Visual Studio Breakpoint

Here we have added a simple breakpoint. Our program will break when we hit the breakpoint for the first time. Let’s say that we want our breakpoint to hit at 50 iteration of our loop. To do this we can right click on the breakpoint circle on left hand side and click Hit Count.

Visual Studio Breakpoint 

When we click on Hit Count we get a dialog where we can set the conditions when our breakpoint should be hit.

Visual Studio Breakpoint

In our case we will select “break when the hit count is equal to” and enter 50.

Visual Studio Breakpoint

If we now run our program we will hit the breakpoint when value of i = 49. Remember that we started i at 0 so when i is equal to 49 we are on the 50th iteration.

This comes in handy when debugging large loops and we are only interested in breaking when we have processed X number of iterations.

Tagged with:
 

2 Responses to Create Breakpoints With Hit Count In Visual Studio 2008

  1. Deepak says:

    Thanks Stafford.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>