Debug In C#

What is Debug?

Debugging is the process of finding errors during application execution. It does not mean syntax errors, with which the application cannot be compiled, but on logic errors.

Logic errors can only be noticed during application execution. See the following method.
  1. private double Divide(double a, double b)  
  2. {  
  3.    return a / b;  
  4. }  
This is a method used to share two double types. The method accepts two parameters of the type double and returns the result of the partition. For the displayed code it can be said that it does not contain syntax errors. However, what if the method, as the second parameter, passes the value 0?

Zero sharing is not possible, and such a scenario will produce an exception. Here, a logical error occurs and the occurrence of such errors must be prevented. By adding simple checks, the errors can be prevented.
  1. private double Divide(double a, double b)  
  2. {  
  3.    if ( b == 0)  
  4.    return 0;  
  5.    return a / b;  
  6. }  
Debugging helps precisely in these and similar situations when we cannot determine the origin of the logical error in the code. In such situations, Debug allows us to stop execution on a particular line, pass through the code line by line, and test the state of the application.

Debug Background code 

The basic tool for achieving Debug application code in the Visual Studio development environment is break points.

Break points are set by clicking on the margin on the left side of the code in the appropriate line.


In the case of the image, the break point is placed on the third line within the Main method.

To stop Debug, it is sufficient to select the Start option, or the Start Debugging option from the Debug menu, or simply the F5 key. 

 
(Start Debug)

After running, Debug execution will be stopped in the line where the breakpoint is defined.

Further management of execution can be done by using the Step Into, Step Over and Step Out keys.

 

In the case where Debug is demonstrated, the second class method and the loop were not accidentally cited.

Order Step Over simply moves from one line to another without entering the method.

This means that in our case, using the Step Over command, we will go through every line in the main methods and five times through for the loop.

If we want to get into the definition of some of the methods that are being invoked during debugging, the Step Into command can be used. Thus, when an application exits to the line where the ShowMessage method is called, the MyClass class, by calling the Step Into command,  will continue  debugging in the body of the Show Message method.

If we want to return from the ShowMessage method immediately, we can use the Step Out key. Also, when executing the Step Out command within a loop, all loop iterations can be skipped and continue with a code that is defined outside the loop.