Wednesday, 7 December 2016

26 C# Code Stepping

Stepping through the code

In this chapter, we will look into stepping through your code, which is another very essential part of debugging. For the purpose, I have written this simple application: 

namespace DebugTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 5;
            a = a * 2;
            a = a - 3;
            a = a * 6;
            Console.WriteLine(a);
        }
    }
}

It simply manipulates the variable "a" a couple of times and the outputs the final result. Try placing a breakpoint, as described in the previous chapter, on the very first line where a is used (and declared). Now run the application. The execution is stopped and you can hover your mouse over the a, to ensure that what we learned in the previous chapter is in fact true: The variable only contains the default value, because the code that assigns the value (in this case 5), has not been executed yet, but let's change that. From the Debug menu, select the "Step over" option, or even better, use the keyboard shortcut F10. The execution will now proceed to the next relevant line and if you hover your mouse over the a variable, you will now see that it has a value. Try again, and you will see the value change according to the lines being executed one by one, until you have reached the end. 

Okay, so that was pretty basic, but also very useful, as you will realise once you start writing more complicated code. In this example, the flow of the code was very simple, since we stayed within a single function, but what if your code starts spreading over multiple classes and/or functions? Try this example: 

namespace DebugTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 5;
            int b = 2;
            int result = MakeComplicatedCalculation(a, b);
            Console.WriteLine(result);
        }

        static int MakeComplicatedCalculation(int a, int b)
        {
            return a * b;
        }
    }
}

Place a breakpoint on the first line of the Main method and run the application. Now use the "Step over" function to step through each line. As you will see, it moves over the function call without any notice - that's simply how debugging works. Now, try again, from the start, and once you have stepped to the line with the MakeComplicatedCalculation() call, select Debug -> Step into, or use the keyboard shortcut F11. The debugger now steps into the first possible function call, allowing you to step through that function as well. As you can probably imagine, this allows you to step through a complicated block of code, while only entering the function calls that interests you. 

If you step into a function and then realise that you would rather return to the previous context, you use the very logically named option from the Debug menu called "Step out" (keyboard shortcut is Shift+F11). This will return you to your previous context, which obviously means that you can step into as many function calls as you want to, and then find your way back by using the step out option.

No comments:

Post a Comment