Learn C#

Modern, statically typed language for .NET: backends, desktop, games and cloud

32 lessons · 6 hours · 4 levels · covers C# C# 14 / .NET 10

C# is Microsoft's statically typed, object-oriented language for the .NET platform, with strong functional features such as LINQ, lambdas, records and pattern matching. It is used for web backends with ASP.NET Core, Windows and cross-platform apps, Unity games and cloud services. Learning it gives you a productive, fast, well-tooled language with a huge standard library.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics11 lessons · 2 hours in total
2. PracticalUse it productively at work, with the usual idioms22 lessons · 4 hours in total
3. AdvancedThe less common features, performance and trade-offs28 lessons · 5 hours in total
4. ExpertInternals and the edge cases experts know32 lessons · 6 hours in total

The C# tree

C#Run C# with the dotnet CLIRun C# with thedotnet CLIVariables and built-in typesVariables and built-intypesStrings and interpolationStrings andinterpolationConditions and loopsConditions and loopsMethods and parametersMethods andparametersArrays and List<T>Arrays and List<T>Classes and objectsClasses and objectsProperties and initializationProperties andinitializationNull and nullable typesNull and nullabletypesExceptionsExceptionsNamespaces, projects and NuGetNamespaces,projects and NuGetStructs, value and reference typesStructs, value andreference typesInheritance and interfacesInheritance andinterfacesRecordsRecordsGenericsGenericsDictionaries and setsDictionaries and setsDelegates, lambdas and eventsDelegates, lambdasand eventsLINQLINQPattern matching and switch expressionsPattern matching andswitch expressionsasync and awaitasync and awaitIDisposable and usingIDisposable andusingUnit testing with xUnitUnit testing withxUnit
Essentials and Practical shown (22 of 32 lessons); the full list is below. Click a lesson to read it.

All 32 lessons, in order

Basics

  1. Run C# with the dotnet CLI · Essentials · 10 min

    Create, build and run a C# program with the dotnet command line and top-level statements.

  2. Conditions and loops · Essentials · 10 min

    Branch with if and switch, and repeat with for, foreach, while and do-while.

  3. Methods and parameters · Essentials · 12 min

    Define methods with typed parameters, return values, optional and named arguments, and out parameters.

  4. Exceptions · Essentials · 10 min

    Throw and catch exceptions with try/catch/finally, filters, and the right built-in exception types.

  5. Namespaces, projects and NuGet · Essentials · 10 min

    Organize code with namespaces and using directives, and add libraries from NuGet to a .csproj.

  6. Unit testing with xUnit · Practical · 12 min

    Write and run automated tests with xUnit, Fact and Theory attributes, and dotnet test.

Types and data

  1. Variables and built-in types · Essentials · 10 min

    Declare variables with explicit types or var, and know int, long, double, decimal, bool, char and string.

  2. Strings and interpolation · Essentials · 10 min

    Build and manipulate immutable strings with interpolation, verbatim and raw literals, and StringBuilder.

  3. Arrays and List<T> · Essentials · 10 min

    Store sequences in fixed-size arrays or growable List<T>, with indexing, collection expressions and foreach.

  4. Null and nullable types · Essentials · 12 min

    Handle missing values with nullable value types, nullable reference types, ?. and ??.

  5. Structs, value and reference types · Practical · 12 min

    Understand how structs and classes differ in copying, equality, allocation and boxing.

  6. Generics · Practical · 12 min

    Write type-safe reusable classes and methods with type parameters and constraints.

  7. Dictionaries and sets · Practical · 10 min

    Look up values by key with Dictionary<TKey,TValue> and keep unique items with HashSet<T>.

Objects and types

  1. Classes and objects · Essentials · 12 min

    Define classes with fields, constructors and methods, create instances with new, and control access.

  2. Properties and initialization · Essentials · 10 min

    Expose state through auto-properties, computed properties, init-only setters and required members.

  3. Inheritance and interfaces · Practical · 14 min

    Reuse and abstract behavior with base classes, virtual/override, abstract classes and interfaces.

  4. Records · Practical · 10 min

    Use record types for immutable data with value equality, with-expressions and positional syntax.

  5. IDisposable and using · Practical · 8 min

    Release files, connections and other resources deterministically with IDisposable and using declarations.

Lambdas, LINQ and patterns

  1. Delegates, lambdas and events · Practical · 12 min

    Pass behavior as values with Func, Action and lambda expressions, and publish notifications with events.

  2. LINQ · Practical · 15 min

    Query and transform collections with LINQ methods like Where, Select, OrderBy, GroupBy and ToList.

  3. Pattern matching and switch expressions · Practical · 12 min

    Test shapes of data with type, property, relational, list and logical patterns in is and switch expressions.

Async and concurrency

  1. async and await · Practical · 15 min

    Write non-blocking code with Task, async methods and await, and run several operations concurrently.

  2. Cancellation and parallelism · Advanced · 14 min

    Cancel async work with CancellationToken, limit concurrency, and parallelize CPU work with Parallel and channels.

  3. How async/await works inside · Expert · 15 min

    See the compiler-generated state machine, synchronization contexts, ConfigureAwait and ValueTask trade-offs.

Advanced language features

  1. Iterators and yield return · Advanced · 12 min

    Produce lazy sequences with yield return, and understand IEnumerable, IEnumerator and deferred execution.

  2. Extension methods and members · Advanced · 10 min

    Add methods and properties to existing types with extension methods and C# 14 extension blocks.

  3. Advanced generics and variance · Advanced · 12 min

    Use covariance (out) and contravariance (in), static abstract interface members and generic math.

  4. Attributes, reflection and source generators · Advanced · 14 min

    Annotate code with attributes, inspect types at runtime with reflection, and generate code at compile time.

Runtime and performance

  1. Span<T> and Memory<T> · Advanced · 14 min

    Slice arrays, strings and stack memory without copying using Span<T>, ReadOnlySpan<T> and Memory<T>.

  2. The .NET garbage collector · Expert · 14 min

    Understand generations, the large object heap, GC modes and how allocations affect latency.

  3. JIT, tiered compilation and Native AOT · Expert · 14 min

    Learn how IL becomes machine code: tiered JIT, PGO, ReadyToRun, trimming and Native AOT.

  4. Unsafe code and native interop · Expert · 14 min

    Call native libraries with P/Invoke and LibraryImport, and use pointers, fixed and unsafe code safely.