Learn C#
Based on C# 12C# classes, properties, inheritance, records and .NET layout, by example.
new. Covers constructors, this, object initializers and target-typed new().Example: music playlist2EncapsulationKeep values like a balance behind a property with a private set and change them only through methods that check. Also covers init, required and computed properties.Example: bank account3InheritanceInherit with : Parent and change only what differs with virtual and override. Also base, sealed, and how hiding with new is different.Example: staff pay4PolymorphismOne DailyKwh() call runs differently for each object. Also how to ask about types with is and switch patterns.Example: home energy use5Abstractioninterface names the job; abstract class fixes the steps and leaves blanks. Includes default interface methods.Example: backup jobs6Composition, static, recordsAn order that "has" an address and items (composition), class-wide static members, and value-based record types with with.Example: delivery order7Access modifiersWhere public, private, protected, internal, protected internal and private protected are visible, in one table and a game character example.Example: game character+Project structurePick a console app, a web API or a class library and get the .sln, .csproj and Program.cs tree plus an xUnit test project as a zip.zip downloadSuggested order
Start with classes and objects and go in order. Once encapsulation has shown you C# properties (get and set), the later code reads much more easily. Inheritance, polymorphism and abstraction show how classes work together.
Composition and static introduces record, and access modifiers covers internal, which Java does not have. Finish by building a .NET solution folder in project structure.
Where the output comes from
Every example was built and run with the .NET 8 SDK on our server, and the output is pasted as it came out. Step-through values come from real runs too: we inserted a small reader on each marked line. Compiler errors in the mistake boxes are real build errors with only the file name and line number removed.
C# does not run on this page. Run your edited code on your own computer like this.
dotnet new console -o Democd Demo# paste the code into Program.cs, thendotnet runC# version
Text and examples follow .NET 8 (LTS) and C# 12. Installing the .NET 8 SDK gives you C# 12 by default, and the code runs unchanged on newer versions. Syntax added after C# 8 is marked at the top of the page, for example "C# 9+".
Where C# came from
C# was built at Microsoft in the United States alongside .NET. Anders Hejlsberg led the design. The first edition of the Ecma standard ECMA-334 came out in December 2001, and C# 1.0 shipped in January 2002 with Visual Studio .NET 2002. Microsoft open-sourced the compiler (Roslyn) in 2014, and .NET Core, which runs on Linux and macOS too, arrived in 2016. Microsoft still leads the work with the .NET Foundation behind the open-source projects; the newest version is C# 14, released in November 2025.
Windows desktop programs, web servers and APIs on ASP.NET Core, games made with the Unity engine and cloud services such as Azure use C# heavily. Strict type checks and strong help from Visual Studio make it easy for a team to split a large business program into parts. Short scripts tend to be Python, and apps only for Apple devices are usually Swift.
On this site, start with classes and objects, and hold C#-only syntax such as properties and records up against the Java examples. Project structure gives you a .sln and .csproj skeleton to try with dotnet run.
Tools that pair well Compare languages C# project structure Learn Java
| Made by | Microsoft (USA), design led by Anders Hejlsberg |
|---|---|
| First release | C# 1.0 in January 2002 · ECMA-334 standard (December 2001) |
| Run today by | Microsoft with the .NET Foundation (open source) |
| Used mostly for | Windows apps, web servers and APIs, Unity games, cloud |
Questions people ask
I am new to C#. Where should I start?
Start with classes and objects and go in order. The pages assume you know basics such as variables, if and for. Coming from Java, the biggest differences are properties (get and set) in encapsulation and internal in access modifiers.
Which version do the examples need?
They were built and run with the .NET 8 SDK (C# 12) and work unchanged on newer SDKs. Syntax added after C# 9 is marked at the top of each page.
How do I run an example on my computer?
Create a project with dotnet new console, replace Program.cs with the example and run dotnet run. Visual Studio and VS Code open the same project.