43 lessons

C# Tutorial for Beginners

Learn C# programming and .NET step by step, from your first console app to LINQ, async/await and ASP.NET web APIs, with clear C# examples, exercises and quizzes.

Browse all 43 lessons by stage

Lessons
43
Code examples
92
Exercises
143
Quiz questions
161

C# course at a glance

Who it's for

Complete beginners, students, Unity game developers who want to understand the language properly, and developers moving to .NET from Java, Python or JavaScript.

Prerequisites

None for the core lessons. Basic computer skills are enough, and the in-depth guides list the lessons to finish before you start them.

What you need

The free .NET SDK (version 8 or later) on Windows, macOS or Linux, and an editor such as Visual Studio Community, VS Code with the C# Dev Kit, or Rider.

How long it takes

About two to three months for the 25 core lessons at five hours a week, plus a few weeks for the application guides behind a first web API.

C# roadmap: beginner to advanced

Work through the 25 core chapters in the first three stages in order, then use the reference stages to go deeper. Each reference lesson links to its prerequisite and includes examples, practice and a quiz.

1

Beginner

Your first program, variables and types, operators, strings, input and output, if and switch, loops and methods.

  1. 1IntroductionFind out what C# is and what it's used for, set up the .NET SDK, run your first program with Console.WriteLine, and learn how statements, comments and top-level programs work.
  2. 2Variables & TypesStore values in variables, choose the right built-in type (int, long, double, decimal, bool, char, string), use var for type inference, declare constants, and convert between types.
  3. 3Operators & MathCalculate with arithmetic operators, understand integer division and remainders, update variables with compound assignment and ++, compare values, combine conditions with && and ||, and use the Math class.
  4. 4StringsWork with text in C#: join and interpolate strings, use common methods like ToUpper, Contains, Replace, Split and Trim, format numbers, write escapes and raw strings, and build text efficiently with StringBuilder.
  5. 5Input & OutputBuild interactive console programs: read what users type with Console.ReadLine, handle empty input and nulls, convert text to numbers safely, keep asking until input is valid, and format output clearly.
  6. 6if & switchMake decisions in C# with if, else if and else, write short choices with the ternary operator, choose between many cases with switch statements and switch expressions, and use patterns for ranges and types.
Show all 8 lessonsShow fewer
  1. 7LoopsRepeat work with for, foreach, while and do-while loops, control them with break and continue, nest loops for tables and grids, and pick the right loop for each job.
  2. 8MethodsOrganise code into reusable methods, pass data in with parameters, return results, use optional and named arguments, overload methods, return several values with tuples and out, and write short expression-bodied methods.
2

Intermediate

Arrays, lists and dictionaries, classes, properties and constructors, inheritance, interfaces, structs and records, enums and exceptions.

  1. 9ArraysStore fixed-size collections of values in arrays, read and change items by index, loop over them, sort, search, copy and resize them, slice with ranges, and use two-dimensional arrays for grids.
  2. 10Lists & DictionariesUse the generic collections you'll need every day: List<T> for ordered items that grow and shrink, Dictionary<TKey, TValue> for fast lookups by key, and HashSet<T> for unique values.
  3. 11Classes & ObjectsModel real things with classes, create objects with new, give them fields and methods, protect their data with private and public, and understand reference types, null and static members.
  4. 12Properties & ConstructorsExpose object data safely with properties, validate values in setters, make data read-only with init and private set, compute values on the fly, and set objects up correctly with constructors, required members and primary constructors.
  5. 13Inheritance & PolymorphismShare code between related classes with inheritance, call base constructors, override virtual methods to change behaviour, treat different objects through one base type with polymorphism, and use abstract and sealed classes.
  6. 14InterfacesDefine contracts with interfaces, implement several on one class, program against interfaces instead of concrete classes, use built-in interfaces like IComparable and IDisposable, and swap implementations for testing.
Show all 9 lessonsShow fewer
  1. 15Structs & RecordsUnderstand value types and reference types, create small value types with struct, model immutable data with records, compare by value, copy with changes using with, and choose between classes, structs, records and tuples.
  2. 16EnumsReplace magic numbers and strings with enums, give cases explicit values, switch over them, convert enums to and from strings and numbers safely, loop over all values, and combine options with [Flags].
  3. 17ExceptionsHandle run-time errors with try, catch and finally, catch specific exception types, throw your own exceptions with clear messages, create custom exception classes, use exception filters, and decide when exceptions are the right tool.
3

Advanced

Generics, LINQ, delegates and lambdas, async/await, files and JSON, nullable types and patterns, unit testing and minimal APIs.

  1. 18GenericsWrite type-safe code that works with any type using generic methods and classes, add constraints with where, build generic interfaces like a repository, and understand how List<T> and Dictionary<TKey, TValue> work.
  2. 19LINQQuery and transform collections with LINQ: filter with Where, project with Select, sort with OrderBy, group with GroupBy, combine with Join, summarise with Count, Sum and Average, and understand deferred execution.
  3. 20Delegates & LambdasTreat methods as values with delegates, write inline functions with lambda expressions, use the built-in Func, Action and Predicate types, capture variables in closures, and notify other code with events.
  4. 21async & awaitKeep apps responsive and servers scalable with asynchronous code: write async methods that return Task, await them, run work in parallel with Task.WhenAll, cancel with CancellationToken, and call web APIs with HttpClient.
  5. 22Files & JSONRead and write text files, process large files line by line, build paths that work on every operating system, manage folders, and save and load objects as JSON with System.Text.Json.
  6. 23Nullable & PatternsPrevent null reference errors with nullable reference types and null operators, and write expressive conditions with type, property, relational, list and positional patterns in is and switch expressions.
Show all 8 lessonsShow fewer
  1. 24Unit TestingWrite automated tests for C# code with xUnit: create a test project, write Fact and Theory tests, assert results and exceptions, follow Arrange-Act-Assert, test async code, and replace dependencies with fakes.
  2. 25Minimal APIsBuild a JSON web API with ASP.NET Core minimal APIs: map GET, POST, PUT and DELETE endpoints, bind route, query and body values, return proper status codes with Results, validate input, and use dependency injection.
4

Language and library reference

Conversions and culture, dates and time zones, regex, text and UTF-8, projects and NuGet, equality, extensions, iterators, disposal, variance and reflection.

  1. 26Conversions and CultureConvert C# numeric values safely, distinguish range from precision, parse culture-aware input, and format stable decimal values for storage.
  2. 27Dates and TimesChoose DateTimeOffset, DateOnly and TimeOnly in C#, parse explicit date formats, handle time zones, and distinguish elapsed time from calendar rules.
  3. 28Regular ExpressionsUse C# Regex for bounded validation and extraction, understand anchors and named groups, and prevent ambiguous patterns and unbounded matching.
  4. 29Unicode and EncodingBuild C# text efficiently, distinguish UTF-16 units from Unicode characters, enumerate runes, and encode or decode UTF-8 with explicit error handling.
  5. 30Projects and NuGetOrganize C# code into namespaces and projects, add explicit project references, understand NuGet restore, and keep framework and dependency versions visible.
  6. 31Equality and HashingDefine C# value equality consistently, keep hash keys stable, use explicit string comparers, and distinguish equality from sorting and reference identity.
Show all 11 lessonsShow fewer
  1. 32Extension MethodsWrite traditional C# extension methods, understand namespace lookup and static dispatch, handle null explicitly, and design focused reusable helpers.
  2. 33Iterators and YieldCreate C# iterators with yield return, predict when work and exceptions occur, control repeated enumeration, and understand cleanup during early exit.
  3. 34Disposal and OwnershipUse using and await using in C#, distinguish garbage collection from resource cleanup, define ownership, and implement safe idempotent disposal.
  4. 35Generic VarianceUnderstand C# in and out variance, safely widen producers and narrow consumers, and distinguish generic-interface variance from class and array behavior.
  5. 36Attributes and ReflectionDefine C# attributes, inspect public metadata with reflection, distinguish discovery from execution, and account for trimming and AOT deployment limits.

What is C#?

C# (pronounced "C sharp") is a modern, general-purpose language created by Microsoft. It runs on .NET, an open-source platform for Windows, macOS and Linux, and it's used to build web APIs and websites with ASP.NET Core, cloud services, desktop apps, mobile apps with .NET MAUI, and games with Unity, one of the world's most popular game engines.

C# is statically typed, so the compiler catches many mistakes before your program runs, and its tooling is excellent: Visual Studio, VS Code and Rider all offer accurate suggestions and powerful debugging. The language keeps improving every year, with features like records, pattern matching and nullable reference types that make code shorter and safer.

This tutorial has three stages. The beginner stage covers your first program, variables and types, operators, strings, console input and output, decisions, loops and methods. The intermediate stage teaches arrays, lists and dictionaries, classes and objects, properties and constructors, inheritance, interfaces, structs and records, enums and exceptions. The advanced stage covers generics, LINQ, delegates and lambdas, async/await, files and JSON, nullable types and pattern matching, unit testing, and building a web API with ASP.NET Core.

Every lesson has small examples with their output, common mistakes, exercises and a quiz. If you already know Java, much of C# will feel familiar.

How to follow this course

Work through the 25 core lessons in order, because each one builds on the last. After that, the in-depth guides answer the questions real .NET projects raise. The language and library guides cover numeric conversions and culture, dates and time zones, regex, text and Unicode, projects and NuGet, equality and hashing, extension methods, iterators, disposal, generic variance and reflection. The application guides cover thread safety, dependency injection lifetimes, configuration and secrets, structured logging, HttpClient, Entity Framework Core and ASP.NET Core security. Each guide lists the core lessons to finish first.

What you will learn

10 practical skills, built one lesson at a time

  • Set up the .NET SDK and run C# programs with dotnet run
  • Use variables, numeric types, decimal for money, strings and interpolation
  • Read and validate user input safely with TryParse
  • Control programs with if, switch expressions, patterns and loops
  • Write methods with optional and named arguments, overloads and tuples
  • Store data in arrays, lists, dictionaries and sets
  • Design classes with properties, constructors, inheritance, interfaces and records
  • Handle errors with exceptions and write generic, reusable code
  • Query data with LINQ, pass behaviour with lambdas, and write async code
  • Read and write files and JSON, test code with xUnit and build a web API
43 lessons92 examples143 exercises

Practise C# your way

Learning sticks when you use it. Pick a mode.

C# FAQ

Is C# good for beginners?

Yes. C#'s syntax is clear and consistent, the compiler explains mistakes with helpful error codes, and modern features like top-level statements mean your first program is a single line. The skills you learn transfer easily to Java, TypeScript and C++.

What is the difference between C# and .NET?

C# is the programming language. .NET is the platform that compiles and runs it, including the runtime, the standard library and the dotnet command. Other languages such as F# and Visual Basic also run on .NET, but C# is by far the most widely used.

Do I need Windows to learn C#?

No. The .NET SDK runs on Windows, macOS and Linux, and VS Code and JetBrains Rider work on all three. Visual Studio, Microsoft's full IDE, is Windows-only, but everything in this course works with any of these tools.

Is C# used for game development?

Yes. Unity uses C# for all of its game scripts, so it's the language behind a huge number of mobile, indie and console games. The C# you learn here, especially classes, collections and events, is exactly what Unity scripting builds on.

How long does it take to learn C#?

With about an hour a day, most beginners finish the beginner stage in two to three weeks and the whole course in two to three months. Building something alongside the lessons, such as a console budgeting tool or a small web API, is the fastest way to make it stick.

Is this C# course free?

Yes. All 25 core lessons, the 18 in-depth guides, the exercises, the quiz and the cheat sheet are free, with no account or sign-up needed.

Which .NET version does the course use?

The examples are written for .NET 8, a long-term support release, and later. They use modern C# features such as top-level statements, records, pattern matching and nullable reference types, which all need a recent SDK.

Should I learn C# or Java?

Both are statically typed, object-oriented languages with very similar syntax, so the skills transfer well. Choose C# for Unity games, Windows desktop apps or a .NET workplace; choose Java for Android's older codebases or a Java-based team. See the Java tutorial to compare.

Does this course teach Unity?

No. It teaches the C# language and .NET, which is what Unity scripts are written in. Classes, collections, events and async code all carry straight over, so Unity's own tutorials become far easier afterwards.

More tutorials