.NET CLI Basics
The dotnet command-line interface (CLI) is the primary tool for creating, building, running, testing, and publishing .NET applications, and it ships with every SDK installation. It works identically on Windows, Linux, and macOS terminals, which means the exact same commands — dotnet new, dotnet build, dotnet run — work regardless of which OS a developer or CI pipeline uses.
Cricket analogy: Like the standard set of umpire signals (four, six, out) understood identically at every cricket ground worldwide, the dotnet CLI commands behave identically whether you're on a Windows laptop or a Linux CI server.
Creating and Running Projects
dotnet new scaffolds a new project from a template — dotnet new console, dotnet new webapi, and dotnet new classlib are among the most common. Running dotnet new list shows every installed template, and dotnet new install <package> adds third-party templates such as those published by Ocelot or MediatR maintainers. Once a project exists, dotnet run compiles and executes it in one step, automatically restoring NuGet packages first if needed.
Cricket analogy: Like choosing a pre-set fielding template (e.g., a 'death overs' field) before adjusting it for the specific batter, dotnet new webapi scaffolds a ready-made starting structure you then customize.
Building, Testing, and Publishing
dotnet build compiles the project and its dependencies into intermediate output under bin/Debug/<TFM>/ without running it, while dotnet test discovers and executes test projects using frameworks like xUnit, NUnit, or MSTest and reports pass/fail results. dotnet publish produces a deployable output — by default framework-dependent, or self-contained with --self-contained true -r linux-x64 — ready to copy to a server or bake into a Docker image.
Cricket analogy: Like a net practice session (dotnet build compiling without a real match) versus an actual televised match (dotnet run), and finally packing the team kit bag for an overseas tour (dotnet publish), these three commands represent progressive stages of readiness.
# Scaffold, build, test, and publish a Web API project
dotnet new webapi -n OrdersApi
cd OrdersApi
dotnet build
dotnet new xunit -n OrdersApi.Tests -o ../OrdersApi.Tests
cd ../OrdersApi.Tests && dotnet add reference ../OrdersApi/OrdersApi.csproj
dotnet test
cd ../OrdersApi
dotnet publish -c Release -r linux-x64 --self-contained true -o ./publish
dotnet run always rebuilds if source files changed, but it does NOT automatically restore new NuGet packages added directly to the .csproj by hand in some SDK versions — run dotnet restore explicitly if you edit package references outside of dotnet add package.
- The
dotnetCLI ships with the SDK and provides identical commands across Windows, Linux, and macOS. dotnet new <template>scaffolds projects;dotnet new listshows installed templates.dotnet buildcompiles without running;dotnet runcompiles and executes in one step.dotnet testdiscovers and runs test projects built with xUnit, NUnit, or MSTest.dotnet publishproduces deployable output, optionally self-contained with a runtime identifier likelinux-x64.dotnet add packageanddotnet add referencemanage NuGet packages and project references from the terminal.dotnet restoreexplicitly re-resolves NuGet dependencies when needed.
Practice what you learned
1. Which command scaffolds a new ASP.NET Core Web API project?
2. What is the difference between `dotnet build` and `dotnet run`?
3. Which command lists all installed dotnet project templates?
4. Which flag makes `dotnet publish` produce a self-contained deployment for Linux?
5. Which test frameworks can `dotnet test` execute?
Was this page helpful?
You May Also Like
What Is .NET Core?
An overview of .NET Core as Microsoft's open-source, cross-platform runtime and how it evolved into the unified modern .NET.
Project and Solution Structure
How .csproj project files and .sln solution files organize .NET code, dependencies, and multi-project applications.
.NET Runtime vs SDK
The difference between the .NET Runtime (needed to run apps) and the .NET SDK (needed to build them), and when to install each.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics