Get Started
Install the .NET SDK, scaffold a console app, run, test, and build a release binary.
C# — getting started
EXAMPLE
# ===== 1. Install .NET SDK =====
# macOS:
brew install --cask dotnet-sdk
# or download from dotnet.microsoft.com
# Linux:
# Follow distro instructions; e.g. on Ubuntu:
sudo apt install -y dotnet-sdk-8.0
# Windows:
winget install Microsoft.DotNet.SDK.8
# Verify:
dotnet --version
# 8.0.x
# ===== 2. Hello, console app =====
dotnet new console -n Hello
cd Hello
# Program.cs (top-level statements):
Console.WriteLine("hello, C#");
dotnet run
# ===== 3. Tests (xUnit) =====
cd ..
dotnet new sln -n Demo
dotnet sln Demo.sln add Hello/Hello.csproj
dotnet new xunit -n Hello.Tests
dotnet sln Demo.sln add Hello.Tests/Hello.Tests.csproj
dotnet add Hello.Tests/Hello.Tests.csproj reference Hello/Hello.csproj
# Hello.Tests/UnitTest1.cs
using Xunit;
public class MathTests {
[Fact] public void OnePlusOne() => Assert.Equal(2, 1 + 1);
}
dotnet test
# ===== 4. A tiny Web API =====
dotnet new web -n Api
cd Api
# Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/healthz", () => Results.Json(new { ok = true }));
app.Run();
dotnet run
# http://localhost:5000/healthz
# ===== 5. Modern features =====
# Program.cs (records + pattern matching)
public record User(int Id, string Name);
var u = new User(1, "Alex");
var size = u.Id switch {
< 10 => "small",
< 100 => "medium",
_ => "large",
};
# ===== 6. Build for production =====
dotnet publish -c Release -o publish
# Single-file deploy:
dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true
# ===== 7. Package management =====
dotnet add package Newtonsoft.Json
dotnet remove package Newtonsoft.Json
dotnet list package
# ===== Patterns to internalise =====
# - Use the latest LTS (.NET 8); upgrade once a year
# - Nullable reference types enabled (<Nullable>enable</Nullable>)
# - async / await everywhere at I/O boundaries
# - Records for DTOs
# ===== Pitfalls =====
# - Mixing Framework / Core / .NET 5+ codebases; use only modern .NET
# - Forgetting to enable nullable -> NullReferenceException pain
# - Using .Result on Tasks (deadlocks)
# - Catching Exception broadly
Why it matters
Install the SDK, dotnet new, dotnet run, dotnet test — the same four verbs cover console apps, web APIs, libraries, and tests. The runtime is fast; the tooling is mature; the language has aged better than most. Reach for .NET 8+ on anything new.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
# Install .NET SDK dotnet --version dotnet new console -n hello && cd hello dotnet runTry it Yourself »
Discussion
Loading…