The CLI, and what is not here
The commands
dotnet with no arguments lists its commands, and the list is the map of the rest of the course:
build Build a .NET project.
clean Clean build outputs of a .NET project.
format Apply style preferences to a project or solution.
msbuild Run Microsoft Build Engine (MSBuild) commands.
new Create a new .NET project or file.
nuget Provides additional NuGet commands.
pack Create a NuGet package.
package Search for, add, remove, or list PackageReferences for a .NET project.
publish Publish a .NET project for deployment.
reference Add, remove, or list ProjectReferences for a .NET project.
restore Restore dependencies specified in a .NET project.
run Build and run a .NET project output.
sdk Manage .NET SDK installation.
solution Modify Visual Studio solution files.
test Run unit tests using the test runner specified in a .NET project.
tool Install or manage tools that extend the .NET experience.
The commands this course uses most are new, build, run, publish and format. pack and test need a package feed for their usual work, which this container does not have, and the testing group of the course shows the real failure of dotnet test.
dotnet format
dotnet format is a formatter and a small fixer for the analyzers of the SDK. It is installed, and it prints its own version:
dotnet format --version
10.0.303-servicing.26377.103+e730f1db756d11c93f246830ba7b94ee6fcf4b94
The two forms that matter are dotnet format, which rewrites the files, and dotnet format --verify-no-changes, which changes nothing and leaves status 0 when the files are already formatted and 2 when they are not. The lesson on dotnet format runs both on a file that needs it. It is the linter of this course: the SDK has no separate style tool, and the analyzers it carries are the subject of the lesson on analyzers.
The language version
A trick that costs nothing and works on every SDK: the compiler reports its own version and the C# version it speaks when the source contains a preprocessor error that names them.
#error version
dotnet build
Program.cs(1,8): error CS1029: #error: 'version'
Program.cs(1,8): error CS8304: Compiler version: '5.6.0-2.26377.103 (e730f1db756d11c93f246830ba7b94ee6fcf4b94)'. Language version: 14.0. Compiler path: '/usr/lib/dotnet/sdk/10.0.303/Roslyn/bincore/Microsoft.CodeAnalysis.CSharp.dll'.
Build FAILED.
The SDK 10.0.303 speaks C# 14.0, and the compiler is the Roslyn
assembly inside the SDK folder, not a separate csc
command. A project may pin an older language with
<LangVersion>12</LangVersion>, which is
what a file written for C# 12 needs when it is moved into a project of this
SDK.
The block has no Console call and no
Main, which is why the tools of this course leave it
alone: a block that does not build a program is a fragment, and a fragment
with a deliberate #error is not built.
What is not in this image
Several tools a C# programmer expects are not in this container, and the course says so instead of inventing their output. Each line is a real command -v of this machine:
csc: not found
msbuild: not found
nuget: not found
dotnet-dump: not found
dotnet-trace: not found
dotnet-counters: not found
dotnet-script: not found
- csc and msbuild exist inside the SDK as assemblies, and dotnet build calls them. They are not commands on the PATH, so a line that starts with csc is not a build this container can run.
- dotnet-dump, dotnet-trace and dotnet-counters are global tools: they are installed with dotnet tool install -g from a NuGet feed, and this container has no network. The lesson on debugging without an IDE shows what is left: Debug.WriteLine, a stack trace and the DOTNET_ environment variables.
- dotnet-script is a community tool for running a single C# file, and it is also a NuGet package.
dotnet fsi is F#
The one interactive shell the SDK carries is dotnet fsi, and it is F# Interactive, not C#:
dotnet fsi --help
Microsoft (R) F# Interactive version 15.2.303.0 for F# 10.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
Usage: dotnet fsi <options> [script.fsx [<arguments>]]
A C# REPL does not exist in the SDK: there is no csi and no dotnet script that works offline. A C# program that reads a line, evaluates it and prints a result is the way to get one, and the capstone B of this course builds exactly that: a small expression language with a REPL is the subject of its REPL lesson.
Exercises
- Run dotnet --help and find the command that adds a project reference. Which arguments does it take?
- Put #error version in a project with <LangVersion>12</LangVersion> and read the version the compiler reports.
- Run dotnet format --verify-no-changes on a project with a badly indented file. What is the exit status?
- Try dotnet tool install -g dotnet-script and read the error. Which feed did it try to reach?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which command creates a project from a template?
Answer
dotnet new
dotnet new writes a project or a file from a template; the other names are not commands of the SDK.
-
Which C# version does the SDK 10.0.303 speak?
Answer
14.0
The #error version trick reported Language version: 14.0 for this SDK.
-
What does this build report?
#error versionAnswer
error CS8304: Compiler version: ... Language version: 14.0.
The preprocessor reports CS1029 for the #error and CS8304 with the compiler and language versions.
-
What does dotnet format --verify-no-changes do?
Answer
Reports unformatted files in the exit status and changes nothing
The verify form leaves the files alone and exits 0 when they are formatted and 2 when they are not.
-
What does this program print?
Console.WriteLine("a".Length + 1);Answer
2
The length of a one-character string is 1, and adding 1 gives 2, which is printed as a number.
-
Which of these commands is not on the PATH of this container?
Answer
csc
The compiler is an assembly inside the SDK; csc is not a command, and the three dotnet commands are installed.
-
What is dotnet fsi?
Answer
F# Interactive, an F# shell, not a C# one
fsi starts F# Interactive; the SDK has no C# REPL, so a C# shell has to be a program.
-
How is a global dotnet tool such as dotnet-dump installed?
Answer
dotnet tool install -g <name>, from a NuGet feed
Global tools are NuGet packages, so the install needs a feed and this container, with no network, cannot do it.
-
What does this program print?
var text = "dotnet"; Console.WriteLine(text.ToUpper()); Console.WriteLine(text.Length);Answer
DOTNET / 6
ToUpper returns the upper-case copy and leaves the original alone, and dotnet is six characters long.
-
Which command packs a project into a NuGet package?
Answer
dotnet pack
dotnet pack writes the package; nuget push uploads one that exists, and the other two forms do not exist.
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