.NET cheat sheet
A scannable .NET reference: 16 short snippets across 7 topics, each linking back to the lesson it came from.
At a glance
| Topic | What it covers | |
|---|---|---|
| .NET SDK and project files | The runtime is what executes a compiled .NET application. The SDK is a superset: it bundles a runtime plus the C# | lesson |
| The dotnet CLI workflow | A solution (.sln) is a grouping file for related projects — it is not required to build anything, but it gives the CLI | lesson |
| Publishing and deployment | A multi-stage Dockerfile keeps the SDK out of the runtime image. The build stage restores against the lock file | lesson |
| What .NET is: runtime, libraries and support policy | Publishing a framework-dependent app produces a small DLL that needs a matching runtime installed. Publishing | lesson |
| NuGet and dependency management | NuGet resolves the union of every requirement in the graph and picks the lowest version that satisfies them all. A | lesson |
| Testing .NET code | xUnit setup, fixtures, the assertions that describe intent, and choosing what to mock so the tests stay useful | lesson |
| Writing and publishing NuGet libraries | Package metadata, symbol packages, semantic versioning and how to validate a package before anyone depends on it | lesson |
Quick snippets
.NET SDK and project files
SDK versus runtime
dotnet --list-sdks
# 8.0.404 [C:/Program Files/dotnet/sdk]
# 9.0.100 [C:/Program Files/dotnet/sdk]
dotnet --list-runtimes
# Microsoft.AspNetCore.App 8.0.11 [C:/Program Files/dotnet/shared/Microsoft.AspNetCore.App]
# Microsoft.NETCore.App 8.0.11 [C:/Program Files/dotnet/shared/Microsoft.NETCore.App]
Pinning the SDK version
{
"sdk": {
"version": "8.0.404",
"rollForward": "latestFeature"
}
}Full lesson: .NET SDK and project files →
The dotnet CLI workflow
Scaffolding a solution
dotnet new sln -n Shop
dotnet new classlib -n Shop.Core -o src/Shop.Core
dotnet new webapi -n Shop.Api -o src/Shop.Api -f net8.0
dotnet new xunit -n Shop.Tests -o tests/Shop.Tests
dotnet sln add src/Shop.Core src/Shop.Api tests/Shop.Tests
dotnet add src/Shop.Api reference src/Shop.Core
dotnet add tests/Shop.Tests reference src/Shop.Core
Packages, restore and lock files
dotnet add src/Shop.Api package Microsoft.EntityFrameworkCore.Sqlite
dotnet add src/Shop.Api package Microsoft.EntityFrameworkCore.Sqlite --version 8.0.11
dotnet remove src/Shop.Api package Newtonsoft.Json
dotnet list package # direct dependencies
dotnet list package --outdated # newer versions available
dotnet list package --vulnerable # known CVEs in the graph
dotnet restore # download everything up front
The inner loop
dotnet build # Debug
dotnet build -c Release # optimised, warnings still checked
dotnet build --no-restore # skip restore when nothing changed
dotnet run --project src/Shop.Api
dotnet run --project src/Shop.Api -- --port 5001 # args after -- reach the app
dotnet test # discover and run every test project
dotnet test --filter "FullyQualifiedName~Cart" # narrow to one class
dotnet watch run --project src/Shop.Api # hot reload on file save
dotnet format # apply the repo .editorconfig rulesFull lesson: The dotnet CLI workflow →
Publishing and deployment
Choosing a publish mode
# framework-dependent: the default production choice
dotnet publish src/Shop.Api -c Release -o out
# self-contained for a host you do not control
dotnet publish src/Shop.Api -c Release -r linux-x64 --self-contained true -o out
# container image that runs on the runtime-only base image
dotnet publish src/Shop.Api -c Release -r linux-x64 --self-contained false -o out
Trimming and native AOT
<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>partial</TrimMode>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<StackTraceSupport>false</StackTraceSupport>
</PropertyGroup>
Trimming and native AOT
// reflection-based serialisation is the classic AOT casualty
// prefer a source-generated context
[JsonSerializable(typeof(Order))]
[JsonSerializable(typeof(List<Order>))]
internal partial class ShopJsonContext : JsonSerializerContext
{
}
// register it once
builder.Services.ConfigureHttpJsonOptions(o =>
o.SerializerOptions.TypeInfoResolverChain.Insert(0, ShopJsonContext.Default));Full lesson: Publishing and deployment →
What .NET is: runtime, libraries and support policy
The parts of the platform
# What is installed, and which runtimes a machine actually has
dotnet --version
dotnet --list-sdks
dotnet --list-runtimes
# The same information, machine readable, for a build script
dotnet --info --json | head -40
# What a published application was actually built against
dotnet myapp.dll --info 2>/dev/null || true
LTS, STS and picking a target
<!-- Pin the runtime band a machine may use -->
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RollForward>LatestMinor</RollForward>
</PropertyGroup>
LTS, STS and picking a target
{
"sdk": { "version": "8.0.400", "rollForward": "latestFeature" }
}Full lesson: What .NET is: runtime, libraries and support policy →
NuGet and dependency management
Versioning and how resolution works
# See what was actually resolved, including transitives
dotnet list package --include-transitive
# See what a package is pulling in and why
dotnet nuget why MyApp.csproj Microsoft.Extensions.Logging
# Disable a floating version before it surprises you
dotnet list package --outdated --include-transitive
# Vulnerability audit, which is also part of restore in recent SDKs
dotnet list package --vulnerable --include-transitive
Central package management
<!-- In each project file, no Version attribute: one place to change it -->
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="xunit" />
</ItemGroup>
Central package management
# The lock file pins the full graph; commit it and restore from it in CI
dotnet restore --locked-mode
# After an intentional upgrade, regenerate the lock files
dotnet restore --force-evaluate
git add "**/packages.lock.json"Full lesson: NuGet and dependency management →
Testing .NET code
Mock the boundary, not your own code
# Run everything
dotnet test
# Run one class while iterating
dotnet test --filter FullyQualifiedName~PaymentServiceTests
# Run by trait, which is how you split fast from slow in CI
dotnet test --filter Category=Unit
# With coverage, for a report rather than a gate
dotnet test --collect:"XPlat Code Coverage" --results-directory ./TestResultsFull lesson: Testing .NET code →
Writing and publishing NuGet libraries
Packing, validating and releasing
# Validate a package automatically on every pull request
- name: Pack and check
run: |
dotnet pack -c Release -o ./artifacts
# A package without a readme or with a placeholder version is a defect
dotnet tool run dotnet-validate --package ./artifacts/*.nupkgFull lesson: Writing and publishing NuGet libraries →
FAQ
Is this .NET cheat sheet free to use?
Where do the examples come from?
How do I go deeper than a cheat sheet?
Related cheat sheets
ASP.NET Core LINQ WinForms WPF
Last refreshed 2026-09-27.