The .NET CLI (dotnet) is the command-line interface used to create, build, run, test, and manage .NET applications.
BBrace Tip:
This is the central command tool for the entire .NET ecosystem - remember that.
It is broader than Cargo in Rust or NPM in JavaScript.
dotnet --versionShows the currently active SDK version.
Example:
9.0.100dotnet --infoDisplays:
- installed SDKs
- runtimes
- operating system
- architecture
- environment paths
dotnet --list-sdksdotnet --list-runtimesdotnet new consoleCreates a basic CLI app.
dotnet new console -o MyApp-o means:
output directory
This creates:
MyApp/and places the project there.
cd MyAppdotnet new webapiUsed for backend/API development.
dotnet new mvcdotnet new classlibUseful for reusable libraries/packages.
dotnet new listShows available templates:
- console
- webapi
- mvc
- blazor
- xunit
- maui etc.
dotnet runThis:
- builds the project
- launches the executable
dotnet run --no-buildUseful for faster iteration.
dotnet buildCompiles source code into:
- DLLs
- EXEs
- intermediate artifacts
dotnet build -c Release-c means:
configuration
Usually:
- Debug
- Release
dotnet publishCreates deployable output.
dotnet publish -c Releasedotnet publish -r win-x64 --self-contained trueThis bundles:
- your app
- .NET runtime
into a standalone executable.
dotnet add package Newtonsoft.JsonInstalls external libraries from NuGet.
dotnet remove package Newtonsoft.Jsondotnet restoreDownloads missing packages.
dotnet new xunitCreates unit testing project.
dotnet testdotnet cleanRemoves:
/bin/obj
generated files.
dotnet new slndotnet sln add MyApp.csprojcsiREPL = Read Eval Print Loop
Lets you execute C# code interactively.
Example:
> 5 + 5
10csi may require:
- Visual Studio
- Roslyn tools
- separate installation
depending on environment.
A very popular scripting tool.
Install:
dotnet tool install -g dotnet-scriptRun script:
dotnet script hello.csxInteractive mode:
dotnet scriptUseful for:
- quick experiments
- automation
- scripting
- teaching
dotnet tool install -g <tool-name>dotnet tool list -gdotnet tool update -g <tool-name>Typical developer flow:
dotnet new console -o LibraryApp
cd LibraryApp
dotnet runThen later:
dotnet add package Newtonsoft.Json
dotnet build
dotnet publish -c ReleaseIf you're new to C#, focus first on:
| Command | Purpose |
|---|---|
dotnet --version |
Check SDK |
dotnet new console |
Create app |
dotnet run |
Run app |
dotnet build |
Compile app |
dotnet add package |
Install libraries |
dotnet restore |
Download dependencies |
dotnet publish |
Create deployable app |
csi |
Interactive C# shell |
dotnet script |
Run scripts interactively |