Many a times, we wish to debug our code based on some condition, we can do this by using code statements in Visual Studio.
There is no need to keep a break-point explicitly, we can use the "Break" method present in `System.Diagnostics.Debugger` class. Here is a short example with console application, but we can do the same for all types of projects built with .Net framework.
- static void Main(string[] args)
- {
- Console.WriteLine(2);
- int x = 10;
- Console.WriteLine(x);
- if (x == 10)
- System.Diagnostics.Debugger.Break();
- Console.WriteLine(1);
- }
Here, when we run the application in Debug mode, breakpoint will automatically hit the line# 7. Thus, this will be helpful when we wish to debug only when some condition is satisfied.