Build, run and publish
The .NET CLI has separate commands for dependency preparation, compilation, local execution and deployment packaging. dotnet build creates build output, dotnet run runs a project, and dotnet publish prepares a deployment folder. A successful build alone does not demonstrate correct application behavior.
Restore once in a controlled workspace
Restore resolves the framework and package dependencies used by the build. Many CLI commands restore implicitly unless explicitly told not to. Even a project without package references can need SDK reference packs and restore metadata. Use the included NuGet.Config with cleared package sources only for this dependency-free fixture with the matching packs installed. If a required pack is missing, stop and obtain it through an approved process; do not silently add remote feeds.
Build, then execute
Use a Debug build while learning. The build generates the app assembly and accompanying files under the project output path, commonly bin/Debug/net10.0. A DLL is a compiled assembly, not source text. Run the existing build using --no-build --no-restore only after a successful matching build. After editing code, rebuild before using those options; otherwise the old output can run. Read build diagnostics before trying to fix runtime behavior.
Prepare a release without deploying
For the fixture, publish in Release configuration explicitly, framework-dependent. This output includes the application and its required app files, but expects a compatible installed runtime. A self-contained deployment bundles the .NET runtime for a specified target platform and needs the corresponding packs; native OS dependencies can still matter. dotnet publish does not upload the application. Keep the whole publish directory together. dotnet test runs tests for an appropriately configured test project; it does not invent tests for a console app. Testing frameworks are covered later.
Examples and walkthroughs
The expected program stdout is a single line, 20. SDK build messages are not program output.
Expected output (reasoned, not executed):
20dotnet restore --configfile NuGet.Configdotnet build -c Debug --no-restoredotnet run -c Debug --no-build --no-restoreRun from the project directory, after copying the supplied offline configuration. Stop if any command fails. Expected program output: 20. Build paths and diagnostics are environment-specific.
dotnet publish -c Release --no-restore --no-self-containeddotnet bin/Release/net10.0/publish/ShelfCounter.dllThis assumes the project is named ShelfCounter.csproj and has default output paths and no custom assembly name. The compatible runtime is required. Expected program output: 20. Nothing is deployed to a server.
Common mistake and correction
If you edit Program.cs and run with --no-build, you might see old output. Rebuild, then run. Do not use --no-restore as a fix for a missing assets file. If restore has not succeeded, build has insufficient dependency information. Never treat a publish folder as evidence of production readiness.
Try it yourself
In the reviewed ShelfCounter fixture, first predict 20. Change 12 + 8 to 15 + 7, rebuild Debug, run, and publish Release. Record the two program outputs and explain why the Release output also needs a runtime.
Next: Declaring variables and constants. These examples are static study material; actual C# compilation and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Build, run and publish — knowledge check
Apply the lesson concepts to the following scenarios.
Which operation resolves dependencies and creates restore metadata?
You edit source, then run with --no-build. What can happen?
A build succeeded. What does that establish?
What does a framework-dependent publish require on the destination?
What does dotnet publish do in this local lab?