Visual Studio breakpoint with conditions

🇧🇷 Versão em português

This is a simple and quick tip, maybe you already know it, but I don’t, even after 10 years working daily with visual studio and C#, so I decided write about it.

If you’ve ever had to debug code that takes several attempts to e…

🇧🇷 Versão em português

This is a simple and quick tip, maybe you already know it, but I don’t, even after 10 years working daily with visual studio and C#, so I decided write about it.

If you’ve ever had to debug code that takes several attempts to evaluate behavior on just a certain condition, you already needed something like a breakpoint with conditions. And as its name says, VS provides this feature for us.

To exemplify, I made a simple code that prints 10 random numbers on a console.

Console.WriteLine("Begin");
var rnd = new Random();

for (int i = 1; i <= 10; i++)
{
    Console.WriteLine($"{i}: {rnd.Next(10)}");
}

Console.WriteLine("End");
Console.ReadLine();

If you put a breakpoint, as in the image below, the debug will stop at it all 10 executions of the loop.

Image description

However, you can must observe the loop behavior just when the condition is i==5, in this case, right click on the breakpoint and put your condition.

Image description

The conditions are specified as in the language and you can use any variable available in that scope, in this case I’m using the i variable.

Image description

Image description

Here is a simple example video of the whole process.

I hope you like and if you don’t know this feature, comment there.


Print Share Comment Cite Upload Translate
APA
Felipe Marques | Sciencx (2024-03-29T00:06:15+00:00) » Visual Studio breakpoint with conditions. Retrieved from https://www.scien.cx/2022/06/29/visual-studio-breakpoint-with-conditions/.
MLA
" » Visual Studio breakpoint with conditions." Felipe Marques | Sciencx - Wednesday June 29, 2022, https://www.scien.cx/2022/06/29/visual-studio-breakpoint-with-conditions/
HARVARD
Felipe Marques | Sciencx Wednesday June 29, 2022 » Visual Studio breakpoint with conditions., viewed 2024-03-29T00:06:15+00:00,<https://www.scien.cx/2022/06/29/visual-studio-breakpoint-with-conditions/>
VANCOUVER
Felipe Marques | Sciencx - » Visual Studio breakpoint with conditions. [Internet]. [Accessed 2024-03-29T00:06:15+00:00]. Available from: https://www.scien.cx/2022/06/29/visual-studio-breakpoint-with-conditions/
CHICAGO
" » Visual Studio breakpoint with conditions." Felipe Marques | Sciencx - Accessed 2024-03-29T00:06:15+00:00. https://www.scien.cx/2022/06/29/visual-studio-breakpoint-with-conditions/
IEEE
" » Visual Studio breakpoint with conditions." Felipe Marques | Sciencx [Online]. Available: https://www.scien.cx/2022/06/29/visual-studio-breakpoint-with-conditions/. [Accessed: 2024-03-29T00:06:15+00:00]
rf:citation
» Visual Studio breakpoint with conditions | Felipe Marques | Sciencx | https://www.scien.cx/2022/06/29/visual-studio-breakpoint-with-conditions/ | 2024-03-29T00:06:15+00:00
https://github.com/addpipe/simple-recorderjs-demo