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

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

  1. Run dotnet --help and find the command that adds a project reference. Which arguments does it take?
  2. Put #error version in a project with <LangVersion>12</LangVersion> and read the version the compiler reports.
  3. Run dotnet format --verify-no-changes on a project with a badly indented file. What is the exit status?
  4. 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.

  1. 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.

  2. 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.

  3. What does this build report? #error version

    Answer

    error CS8304: Compiler version: ... Language version: 14.0.

    The preprocessor reports CS1029 for the #error and CS8304 with the compiler and language versions.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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