C#C#

Learn C# – tutorial, cheatsheet & exercises

C# (pronounced "C Sharp") is Microsoft's modern, statically typed general-purpose language. It runs on .NET, is cross-platform (Windows, macOS, Linux) and combines clear object-oriented concepts with functional elements such as LINQ, records and pattern matching.

Typical use cases: game development with Unity, web APIs and websites with ASP.NET Core, desktop applications, cloud services on Azure and enterprise software. The compiler (Roslyn) and the tooling support are excellent – you usually spot errors while you are still typing. This course uses modern C# 12 with .NET 8.

Unity gamesWindows appsWeb APIs (ASP.NET)Enterprise

Hello World in C#

Console.WriteLine("Hello World!");

Thanks to top-level statements, a single line is enough – no class and no Main needed.

How to get started with C#

Install the .NET SDK (version 8 or newer) from dotnet.microsoft.com/download – on the Mac with brew install --cask dotnet-sdk, on Windows with winget install Microsoft.DotNet.SDK.8. Check the installation in the terminal:

dotnet --version

You create a new console project with a single command and start it with dotnet run:

dotnet new console -o HalloCSharp
cd HalloCSharp
dotnet run

The folder contains Program.cs (your code, using so-called top-level statements without a Main method) and HalloCSharp.csproj (project file with the target framework and settings such as Nullable). You add packages with dotnet add package Name, and dotnet watch restarts the program automatically whenever you save.

TipGood editors are Visual Studio Code with the "C# Dev Kit" extension, Visual Studio (Windows/Mac) or JetBrains Rider. You pass command-line arguments through with dotnet run -- Ada --laut.
WarningC# is case-sensitive: Console.WriteLine works, console.writeline does not. Every statement ends with a semicolon.

At a glance

First released2000
CreatorAnders Hejlsberg (Microsoft)
ParadigmMulti-paradigm (OOP, functional)
TypingStatic, strong
ExecutionCompiled to IL, runs on .NET
File extension.cs

Test & practice

Interactive quiz, timed exam with certificate, and exercises with an editor:

C# compared

Hello World: C# comparedVariables: C# comparedConditionals: C# comparedLoops: C# comparedFunctions: C# comparedLists and Arrays: C# comparedDictionaries and Maps: C# comparedClasses and Objects: C# comparedError Handling: C# comparedReading Files: C# compared