You can stop the debugger at a breakpoint after a specific number of breakpoints is reached. Hit count is used to track how many times the debugger stopped at a particular breakpoint. This is extremely useful when you want to stop the debugger after a certain number of iterations; and of course, you don’t have to trail anything using conditional stops.

For example, you have the following block of code, and a breakpoint is placed at line 21, where it prints some value to the console. Now you want to check the value of the 3rd element in the collection before printing.

Must read:



01 How to stop debugger after a specific number of breakpoint hit - https://codeconfig.in

For the small collection given in the example, you can iterate over each element and check the value of the particular element. But is it possible to check when the collection size is large? Suppose you have 1000 items in your collection and you want to check the value of the 300th item. At this point, Visual Studio’s “Breakpoint Hit Count” feature becomes very practical and useful.

21 How to stop debugger after a specific number of breakpoint hit - https://codeconfig.in

By default, it is set to always. So each time a breakpoint is reached, the counter is automatically incremented but the debugger stops at the breakpoint each time. You can also specify the number of hits here to stop the debugger.

For this example, we can select option 2 and set the numeric value to 3 as shown in the image below.

41 How to stop debugger after a specific number of breakpoint hit - https://codeconfig.in
61 How to stop debugger after a specific number of breakpoint hit - https://codeconfig.in

Now click on “OK”, the break point icon will change to a conditional break point icon (additional + Symbol) to indicate some additional condition has been added with the breakpoint.

81 How to stop debugger after a specific number of breakpoint hit - https://codeconfig.in

Now, if you try to debug the application, the debugger will only stop for the items when break point hit count is equal to 3 and this is the same number which you set in above image.

Note: This is not a new feature of visual studio, it has been there from early version of Visual Studio. This may be known to many of you, but beginner may find it interesting.

I hope above will help. You can check more articles on our website under C# section HERE or you can also check same on Microsoft Docs too.

Leave a Reply

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

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can