Once you set up breakpoints, you can use certain commands to step through you code line by line. This allows you to carefully examine what each line of code does and how it affects the program. Create a new Console Application and write the following code that we will use in this lesson.

using System;
 
namespace SteppingThroughCode
{
    public class MyClass
    {
        public static void ShowMessage()
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Have a nice day!");
        }
    }
 
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Line 1");
            Console.WriteLine("Line 2");
            Console.WriteLine("Line 3");
 
            MyClass.ShowMessage();
 
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Hi there!");
            }
        }
    }
}

Example 1 – Code to Demonstrate Stepping Through the Code

Add a breakpoint to the first WriteLine statement of the Main method.

We will now demonstrate how you can step through parts of your code by using the Step OverStep Into, and Step Out commands. Run the program in Debug mode by pressing F5 on your keyboard. The execution will stop at the first breakpoint. We will now start stepping through the code at this breakpoint.

The commands for stepping through your code are located at the Debug toolbar in the toolbars section of the IDE.

Click the Step Over icon  to make the program execute the current line and proceed to the next line of code. Examine the yellow highlight and arrow move 1 step down. Also examine the console for the output, you will see that the first WriteLine statement was executed.

Stepping through your code line by line executes the code one line at a time. Continue clicking the Step Over button until you reach the MyClass.ShowMessage() method.

We will now try to use the Step Into command. This command will enter the currently selected method and will bring you to the definition of that method. You will then be able to execute the lines inside the method one by one.

You can use the Step Over command to go to the next line of the method.

To go outside the method and back to the method that called it, you can use the Step Out command . The Step Out command will execute the remaining part of the method and then you will be brought back to the caller of the method.

Clicking the Step Over button once more will bring us to a for loop statement. Stepping through code will execute the conditions and statements of the loop and after reaching the end of the loop, the cursor will go back to the beginning of the loop to increment the counter and test the condition. If the condition is true, then you will enter the loop again, if not, then you be taken outside the loop and to the next statement.

Stepping through code is useless without knowing how to examine each line of code. The next lesson will teach you how to view values of variables and members of objects.