Loading...

C# Debugging

Find a wrong sum by printing the numbers you stored, then change the one bad operator. Inside Pine.Main, set int x = 10; and int y = 2;. Print each value with a short label. Keep int sum = x - y; on purpose and print sum. Run with dotnet run. The screen should show x=10, then y=2, then 8. A second demo changes that minus to plus, so the last line becomes 12. Leave XML documentation comments for the next page. This lesson is a logic mistake that still runs.
Quality and Wrap-up starts with this page, so the example in Main changes. Inheritance used Car, Truck, and Horn. The equals page built two fresh red strings and printed True, then False, then True: text match, then object.ReferenceEquals, then Equals again. If that file is still the one you run, the screen stays on those bools and this sum never appears. Open the Pine file for this lesson and take the Equals and ReferenceEquals lines out of Main, so the method prints x, y, and sum.
int and Console.WriteLine are already in the course. The new habit is using those prints as a flashlight on a result you do not trust. A thrown exception, from the errors pages, stops the run. This minus does not throw. The program finishes, and the last number is simply the wrong operation.

Label the two numbers

Open the Pine file. The class name stays Pine. Keep using System; at the top. Delete leftover Machine, Truck, and Car types if they bury the sum, leftover string comparisons, leftover Path calls, and any ReadLine line. The program must not wait for typing.
Inside Main, store the two inputs and print them before you trust the sum:
int x = 10;
int y = 2;
Console.WriteLine("x=" + x);
Console.WriteLine("y=" + y);
"x=" + x joins the label to the number, and WriteLine prints that joined text. Then keep a buggy line on purpose for the first demo:
int sum = x - y;
Console.WriteLine(sum);
The labeled lines prove the inputs. The printed 8 proves the operator is wrong for a sum you meant to add.

Pine debug prints with minus bug

csharp
using System;

public class Pine
{
    public static void Main()
    {
        int x = 10;
        int y = 2;
        Console.WriteLine("x=" + x);
        Console.WriteLine("y=" + y);
        int sum = x - y;
        Console.WriteLine(sum);
    }
}

The eight is a clue

From the project folder, run dotnet run. The screen shows x=10, then y=2, then 8. Walk the method once. Main stores ten and two. The first two prints confirm those stored numbers. The third print shows subtraction. Ten minus two is eight, so the screen matches the operator you typed. The bug is not a scrambled variable. The bug is - where a sum needs +.
The build succeeded. A missing semicolon would stop dotnet run with a compiler message and no x=10 line. Here you get all three lines, so you are past syntax and into logic. When the printed inputs look right and the result looks wrong, search the expression that built the result. Do the paper check beside the screen once: 10 and 2 with a minus sign produce 8. That match is the clue.
If the console still shows True and False from the equals file, you are running the comparison demo. Switch to this sum file and run dotnet run again. If you change the operator without printing x and y, you guess in the dark. Keep the labels until the last number matches the inputs you can see. Say the loop while you watch: print the facts, read the wrong result, then change the line that caused it.
A debugger window, with breakpoints and a step button, is a later tool. This page does not open one. Console.WriteLine is enough to see x, y, and sum. Do not add /// documentation comments as the repair. Those comments are the next page, XML docs, and they do not change 8 into 12.

Plus replaces the minus

A second program keeps the same prints of x and y. The sum line uses + instead of -. Nothing else in Main is supposed to move. The last print should show 12.

Pine fixed plus sum

csharp
using System;

public class Pine
{
    public static void Main()
    {
        int x = 10;
        int y = 2;
        Console.WriteLine("x=" + x);
        Console.WriteLine("y=" + y);
        int sum = x + y;
        Console.WriteLine(sum);
    }
}

Run dotnet run again. The screen shows x=10, then y=2, then 12. The inputs did not change. The operator did. That is the debug loop for this shape: see the values, spot the bad operation, fix that one line, run again.
Read the sum line once more: int sum = x + y;. Keep the label prints so a later mistake still shows the inputs. When the last number matches ten plus two, the logic line is fixed for this demo. If both files sit open, the only intentional difference is - versus + on the sum line. Changing x or y at the same time would mix two edits, and the new screen would not tell you which edit mattered.
Paper-check the fixed line the same way. Ten plus two is twelve. The last printed number is 12, so the operator and the screen agree. Leave the labels in the file you keep. Tidying them away before that agreement is how a half-fixed program starts looking mysterious again.
If an equals demo is still nearby, keep it as history. For this lesson, run the sum file so the screen shows numbers, not True and False. One public class Pine in the file is enough. Extra public types from the inheritance pages are not part of this run.

Three checks

The table is the short map of the loop you just walked. First you look at a real run. Then you print the values you distrust. Then you change one clear line and run again. Skipping the look means you edit from a guess. Deleting the prints before the last line reads 12 throws away the evidence.

📊 Beginner debug steps on Pine

Step What you do
Look Run dotnet run and read every printed line
Print Add Console.WriteLine for the values you distrust
Fix Change the bad operator, then run again

Documentation comments stay closed

You can print x and y, keep a minus on purpose, and see 8 after the labels. You can change that operator to plus and see 12. Run Pine with dotnet run for each demo in turn. The screen told you the inputs were fine and the operator was not. Writing /// documentation comments is the next page, XML docs. This page stops before that job. A comment does not replace the plus, and it does not change what dotnet run prints.
Keep the minus demo until 8 is a familiar clue. Keep the plus demo until 12 is the check you expect. Keep documentation-comment files off this run. When the labels still show x=10 and y=2, and the last line is 12, this lesson did its job.
One last pass through the file you leave open: one Pine, one Main, two labeled ints, one sum, no ReadLine, no Equals call, and no /// block. Run dotnet run once more. If the screen still shows the inputs and the result you expect from the operator on the sum line, the debug loop is in place for the next topic.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top