dotnet new, dotnet build and dotnet run
dotnet new
dotnet new writes a project from a template. The template is named on the command line, and -o names the folder to write it into:
dotnet new console -o hello
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /tmp/cs/hello/hello.csproj:
Determining projects to restore...
Restored /tmp/cs/hello/hello.csproj (in 218 ms).
Restore succeeded.
The last three lines are a restore: the SDK reads the project file, works out which runtime packs it needs and writes the file that records the answer. Nothing is downloaded for a console project, because the runtime is already installed; a project that names a NuGet package downloads it here.
What the template writes
Two files are the project, and two folders are its build state:
.
./Program.cs
./hello.csproj
./obj
./obj/hello.csproj.nuget.dgspec.json
./obj/hello.csproj.nuget.g.props
./obj/hello.csproj.nuget.g.targets
./obj/project.assets.json
./obj/project.nuget.cache
Program.cs holds the program and
hello.csproj the project. The
obj folder is written by the restore and by the build,
and a build later adds bin: neither is source, and both
belong in .gitignore.
The content of Program.cs is one line:
Console.WriteLine("Hello, World!");
dotnet build
dotnet build compiles the project and writes the assembly. From the project folder, with no arguments, it finds the one project file of the folder:
dotnet build
All projects are up-to-date for restore.
hello -> /tmp/cs/hello/bin/Debug/net10.0/hello.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:02.95
The line with the arrow is the only line that matters in a successful
build: it names the assembly that was written and where. The configuration
is Debug and the target framework is
net10.0, which are both in the path; the lesson on
the project file
shows where they come from.
A second build of an unchanged project prints the arrow line again, because
the SDK reports the path of the output whether or not it compiled anything.
What tells you that nothing was compiled is the restore line:
All projects are up-to-date for restore. means the
restore had nothing to do, and the compiler compares the timestamps of the
sources with the assembly before it runs.
dotnet run and the exit status
dotnet run builds the project if it is out of date and then starts it in the same terminal:
dotnet run
Hello, World!
Only the program output appears: when the build has nothing to do, its
output is suppressed. To see the build, run
dotnet build first, which is what the rest of the
course does when the compiler is the subject.
The exit status of the program becomes the exit status of
dotnet run. A program that ends with
return 3 from Main, or with
Environment.Exit(3) from anywhere, leaves 3 in
$?:
Console.WriteLine("about to leave");
Environment.Exit(3);
dotnet run
echo "status: $?"
about to leave
status: 3
Environment.Exit ends the process at once: no finally block runs afterwards, and no object is disposed. A program that must clean up returns from Main instead, which the lesson on arguments and exit codes shows with a finally that does run.
clean, restore and the obj folder
Three commands of the same family are worth knowing before the project grows:
- dotnet restore reads the project file and writes obj/project.assets.json. A build runs it first when it is needed.
- dotnet clean deletes the output of the last build, leaving the restore in place.
- dotnet run --no-build starts the assembly that was built last, which is what a lesson that measures startup time wants.
Deleting bin and obj by hand is the same as a clean plus a restore, and it is the safe way to answer "is the build broken or is the state stale".
Exercises
- Create a second project in another folder and compare its .csproj with the first one. Which line names the project?
- Run dotnet build twice in a row. What changes in the output of the second build?
- Make the program leave status 4 and read it with echo $?.
- Delete obj and run dotnet build. Which line of the output tells you that the restore happened again?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does dotnet new console -o hello write?
Answer
A project folder with Program.cs and hello.csproj, and the restore state in obj
The template writes the source, the project file and the files of the restore; the executable comes from a build.
-
Which folder holds the output of a build?
Answer
bin
bin holds the assembly of a build; obj holds the state of the restore and the intermediate files of the compiler.
-
What does this program print, and with which exit status?
Console.WriteLine("about to leave"); Environment.Exit(3);Answer
about to leave / exit status: 3
The line prints, then Exit ends the process with the status it was given, which the shell reads as 3.
-
What does dotnet run print when the project is out of date?
Answer
Only the output of the program
dotnet run builds first and keeps the build quiet unless it fails, so the terminal shows the program output.
-
What does this program print?
int result = Add(2, 3); Console.WriteLine(result); static int Add(int a, int b) => a + b;Answer
5
A top-level program may declare a local function after the statements that call it, and Add returns 5.
-
Which command starts the assembly of the last build without building again?
Answer
dotnet run --no-build
dotnet run --no-build skips the build step; dotnet start does not exist and the others are not commands.
-
A build of an unchanged project prints "All projects are up-to-date for restore." What does it mean?
Answer
The restore had nothing to do, and the build may still compile
That line is the restore step; the compiler still compares timestamps and compiles what changed, and the arrow line reports it.
-
What does this program print?
var name = "world"; Console.WriteLine($"hello, {name.ToUpper()}");Answer
hello, WORLD
An interpolation hole holds an expression, and ToUpper returns the upper-case string.
-
What is the difference between dotnet clean and deleting bin and obj?
Answer
clean deletes the build output and keeps the restore state; deleting both folders removes the restore too
clean removes the output of the last build, while deleting bin and obj also throws away the restore, which the next build repeats.
-
What does this program print?
Console.WriteLine("one"); return; Console.WriteLine("two");Answer
one
return ends the entry point, so only one line prints; the statement after it is a warning (CS0162) at most, not an error.
The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev