Loading...

C# Compiling

Split the run you already trust into two steps: compile the Pine file, then run the program that compile wrote. dotnet build does the first step and prints none of the four lines. When it succeeds, dotnet run --no-build does the second step and shows the screen.
Quality and Wrap-up continues, so the file stays Pine, with Add, left, right, the fixed day, and the die. The summary page ran this same file with one dotnet run and read four lines: left=10 right=2 sum=12, 2026-03-05, roll= with a number from 1 to 6, and inRange=True. This page keeps that file and that screen, and it shows the build by itself, before Main starts. If a line is missing, put the file back first. A build of the wrong file teaches nothing about these four lines.

The file you compile

Open the project from the install page, the PineLab folder that holds the .csproj file. The class inside is still Pine. Keep Add, the four /// lines, the tight sum format, new DateTime(2026, 3, 5), one new Random(), Next(1, 7), and the bool check. Do not add a second class, a second project, or a loop of rolls. The file below is the one both commands use.
You compile this text. You do not compile the screen. The screen appears only when the built program runs.

The Pine file you compile

csharp
using System;

public class Pine
{
    /// <summary>Adds the two stored numbers.</summary>
    /// <param name="left">The first number.</param>
    /// <param name="right">The second number.</param>
    /// <returns>The sum of left and right.</returns>
    public static int Add(int left, int right)
    {
        return left + right;
    }

    public static void Main()
    {
        int left = 10;
        int right = 2;
        int sum = Add(left, right);
        Console.WriteLine("left={0} right={1} sum={2}", left, right, sum);
        DateTime day = new DateTime(2026, 3, 5);
        Console.WriteLine("{0:yyyy-MM-dd}", day);
        Random rng = new Random();
        int roll = rng.Next(1, 7);
        Console.WriteLine("roll={0}", roll);
        Console.WriteLine("inRange={0}", roll >= 1 && roll <= 6);
    }
}

Build prints no program line

From the PineLab folder, run dotnet build. A success report says the build succeeded and lists 0 errors. It does not print left=10 right=2 sum=12. It does not print 2026-03-05. It does not print roll= or inRange=True. Main did not run. The command only checked the file and wrote the program you can run later.
Then run dotnet run --no-build. The screen shows the four lines. The roll is a number from 1 to 6 and may differ from the last run. The other three lines stay. --no-build means "do not compile again; run what the last build wrote." That is why the date is still 2026-03-05 and the sum is still 12: the built program is the file above.
dotnet run without --no-build compiles first when the source is newer, then runs. That single command is why earlier pages showed a screen at once. This page types the two steps so you can see a build that prints nothing from Main.
The folder under bin holds that built program. Its exact name follows the SDK on the computer. The install page allowed SDK 8 or newer, so do not memorize one folder name. You finish this page from the project folder, not by opening bin.

A missing semicolon never reaches Main

As a trial, delete only the semicolon after int left = 10 and run dotnet build again. The report names an error and says the build failed. None of the four lines appear. Main never started, so there is no roll to read and no sum to doubt. Put the semicolon back, run dotnet build again, and wait until the report says the build succeeded.
A compile error and a logic error are different. return left - right; is legal C#. dotnet build succeeds, and dotnet run --no-build then ends the first line in 8. The build did its job: the file compiled. It did not check that the sum is 12. That check is still yours, the same one the debugging page taught by printing the values. Restore return left + right; and build again before you trust the screen.
An unused local can produce a warning while the build still succeeds. A warning is not a failed build. This page's file has no unused local, so a clean report is 0 errors and no need to chase warnings.

An old build can outlive the file

Change return left + right; to return left - right; and do not build. Run dotnet run --no-build. The first line still ends in 12. You are looking at a minus in the editor and a plus in the program that actually runs. --no-build never reads the edit. It runs the last successful build.
Now run dotnet build, then dotnet run --no-build again. The first line ends in 8, because the new build took the minus. Put the plus back, build, and run with --no-build once more. The checked screen is left=10 right=2 sum=12, then 2026-03-05, then roll= with a number from 1 to 6, then inRange=True.
If you delete the bin folder, --no-build has nothing to run. dotnet build writes it again. You do not edit files inside bin or obj. obj holds files the build uses on the way. The source in the project folder is the file you change.

📊 Build and run for the Pine project

Step What you see
Build dotnet build compiles Pine and prints none of the four lines
Run dotnet run --no-build prints the four lines from the last build
Stale a source edit with no new build still runs the old program

Stay in the project folder

Both commands need the folder that holds the .csproj file. On this course that folder is PineLab. If dotnet build says it cannot find a project, you are in the wrong folder. Step into PineLab and run the command again. The class name Pine does not have to match the project name PineLab. dotnet build builds the project. It does not look for a file named after the class.
dotnet --version is the install check. It prints a version line and does not build Pine. dotnet new console creates a project. You already did that on the install page. Do not create a second project here. A Java javac command is not the C# build. This page uses dotnet build.
Build success with 0 errors means the file is legal C#. It does not mean the roll was lucky, and it does not mean the sum is the sum you wanted. Read the four lines after --no-build for that.

Mini project stays shut

You can compile Pine without running it, then run the last build and see the four lines, and you can tell a failed build from a stale one. The next page is the mini project. It builds a small program of its own from the pieces this course already used. This page stops before that job. It does not add a menu, a second class, or a new project folder.
One last pass: the semicolon is back, Add returns left + right, you are in PineLab, dotnet build reports success, and dotnet run --no-build shows left=10 right=2 sum=12, 2026-03-05, roll= with a number from 1 to 6, and inRange=True. When that is true, compiling did its job.

🧠 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