C# Reference

This section documents C# 14 on .NET 10 (LTS), as published at learn.microsoft.com/dotnet/csharp, which is the reference these pages are written and verified against. Features introduced by C# 15 / .NET 11 are still in preview and are always flagged as such — never presented as baseline.

This content was generated with the assistance of AI and should be verified against learn.microsoft.com before being relied on in production.

This section’s bibliography lists the reference material consulted while preparing these pages.

Welcome to the C# reference. C# is a general-purpose, type-safe, object-oriented and functional language running on .NET: managed memory with a garbage collector, a very large standard library, and a compiler that has grown a new release every year without breaking the code that came before it. Designed by Anders Hejlsberg at Microsoft and first released in 2002, it is standardised as ECMA-334 and is today equally at home in web back ends, cloud services, desktop and mobile applications, game engines and high-performance systems code. This section documents C# 14 on .NET 10 (LTS), written and verified against learn.microsoft.com/dotnet/csharp; features introduced by C# 13 and C# 14 are labelled with the version that brought them, and C# 15 / .NET 11 material appears only inside clearly marked preview subsections.

If you are new to C#, read Getting Started and C# and .NET first, then the rest of the Getting started pages in order, followed by Types and object-oriented programming — those two groups are the language’s core. From there the remaining groups (functional programming, error handling and resources, concurrency and asynchrony, advanced language features, and code organisation and tooling) can be read in any order as you need them. Every example in this section, C# 15 preview snippets excepted, compiles cleanly against the .NET 10 SDK with nullable reference types enabled and warnings treated as errors.

Building for the web or a server? The framework material lives in the ASP.NET Reference — ASP.NET Core, MVC, Blazor and Web Forms — which these pages cross-reference rather than duplicate. Coming from Java? The Java Reference covers the same ground for that language, and the pages here call out the differences that most often trip up a Java developer (PascalCase methods, properties instead of getter/setter pairs, value-type struct semantics, is patterns, LINQ, and async/await).

What’s covered

Getting started

  • Getting Started — what C# is, its history from 1.0 to 14, where it is used, installing the .NET 10 SDK, "Hello, World", top-level statements, and the dotnet new/build/run loop.

  • C# and .NET — the CLR, managed code, IL and JIT, the base class library, .NET Standard, the release cadence and support policy, and how C# relates to the runtime it targets.

  • Lexical Structure and Style — identifiers, keywords and contextual keywords, comments and XML documentation, every literal form, statements and blocks, and the formatting conventions.

  • Basic Types and Variables — value versus reference types, the built-in types, var and target-typed new, const and readonly, conversions, boxing, and the default values.

  • Strings and Text — immutability and interning, StringBuilder, interpolation and raw string literals, comparison and culture, spans over text, encodings, and formatting and parsing.

  • Operators and Expressions — the full operator set with precedence and associativity, ?? and ?., ranges and indices, checked/unchecked, and the expression forms.

  • Control Flow — if, loops, the switch statement and the switch expression, jump statements, and the guidance on choosing between them.

  • Methods and Parameters — declarations and expression-bodied members, overloading, optional and named arguments, ref/out/in/params, local functions, and recursion.

Types and object-oriented programming

  • Classes and Objects — fields, constructors and object initializers, properties including the C# 14 field keyword, indexers, static and partial members, primary constructors, and accessibility.

  • Structs and Value Types — copy semantics, readonly struct and ref struct, when a struct is the right choice, and the performance trade-offs.

  • Inheritance and Polymorphism — virtual, override, new and abstract, sealed, constructor chaining, and the members every object inherits.

  • Interfaces — declaring and implementing them, explicit implementation, default interface members, static abstract members and generic math.

  • Records — record class and record struct, positional syntax, value equality, with expressions, deconstruction, and when to prefer a record.

  • Enums — underlying types, explicit values, [Flags] and bitwise combination, conversion and parsing, and the Enum helpers.

  • Generics — generic types and methods, every constraint, variance, generic math, and how generics are implemented at run time.

  • Tuples, Deconstruction and Anonymous Types — ValueTuple syntax and named elements, deconstruction and Deconstruct, and anonymous types.

  • Nullable Types and Null Safety — Nullable<T>, nullable reference types and the flow analysis behind them, the null-related operators, and migrating an existing codebase.

  • Equality and Operator Overloading — reference versus value equality, Equals/GetHashCode contracts, IEquatable<T> and IComparable<T>, and overloading operators and conversions.

  • Extension Members — classic extension methods and the C# 14 extension blocks that add properties and static members, with the design guidance for both.

Functional programming

  • Delegates, Lambdas and Events — delegate types, Func/Action, lambdas and closures, multicast delegates, and the event pattern.

  • Pattern Matching — every pattern form, switch expressions and exhaustiveness, list and property patterns, and how patterns replace type-checking chains.

  • Collections and Iterators — arrays, the generic collections, the interface hierarchy, collection expressions, immutable and frozen collections, and writing iterators with yield return.

  • LINQ — query versus method syntax, deferred execution, the standard query operators by family, IEnumerable<T> versus IQueryable<T>, and writing your own operators.

  • Expression Trees and Dynamic — building, compiling and visiting expression trees, how query providers use them, and dynamic and the DLR.

Error handling and resources

  • Exceptions and Error Handling — try/catch/finally, exception filters, rethrowing correctly, the exception hierarchy, designing custom exceptions, guard helpers, and the alternatives to throwing.

  • Memory Management and Disposal — the generational garbage collector, what allocates, IDisposable and the dispose pattern, using declarations, IAsyncDisposable, SafeHandle, weak references, diagnostics and pooling.

Concurrency and asynchrony

  • Async and Await — the task-based asynchronous pattern, the compiler-generated state machine, ConfigureAwait, composing and cancelling tasks, async streams, and the classic pitfalls.

  • Threads and Synchronization — the thread pool, lock and System.Threading.Lock, semaphores and Interlocked, concurrent collections and channels, the Task Parallel Library and PLINQ, and diagnosing races and deadlocks.

Advanced language features

  • Attributes and Reflection — applying and writing attributes, the compiler-recognised ones, the reflection API, assembly loading, the cost of reflection under trimming and AOT, and source generators as the modern alternative.

  • Unsafe Code, Spans and Performance — Span<T> and Memory<T>, the C# 14 implicit span conversions, stackalloc, ref locals, fields and returns, pointers and fixed, the Unsafe/MemoryMarshal helpers, and benchmarking.

  • Native Interop — P/Invoke with [DllImport] and [LibraryImport], marshalling strings, structs, arrays and callbacks, SafeHandle, library probing, COM interop, and exporting C# to native callers.

  • Preprocessor Directives and Compilation — conditional compilation and its symbols, nullable and #pragma, the C 14 file-based app directives, [Conditional], the compiler options that matter, and how Roslyn is invoked by MSBuild.

Code organisation and tooling

  • Namespaces, Assemblies and Projects — namespaces versus assemblies, internal and [InternalsVisibleTo], the SDK-style project, multi-targeting, NuGet, versioning, and the deployment models up to trimmed Native AOT.

  • Coding Conventions and Documentation — the naming and layout conventions, the framework design guidelines, .editorconfig and analyzers, XML documentation comments, DocFX, and the idioms C# codebases converge on.

  • Build and Tooling — the dotnet CLI, MSBuild and Directory.Build.props, the IDEs, debugging and hot reload, the diagnostic tools, analyzers and source generators, scratchpads, and CI with GitHub Actions.

  • Testing — test projects and dotnet test, xUnit facts, theories and fixtures, the NUnit and MSTest equivalents, the Microsoft Testing Platform, assertions, test doubles, testing async code, and coverage.

  • C# Versions and What’s New — the version table from 1.0 to 14, the C# 13 and C# 14 features in one place, the C# 15 preview list and its timeline, breaking changes and warning waves, and how the language is designed.

Reference

  • Cheat Sheet (PDF) — a single-page, printable C# quick reference covering types and literals, operators, control flow, type and member skeletons, generics, patterns, LINQ, nullability, exceptions, async/await, threading, spans, reflection, the dotnet CLI and the naming conventions.

Bibliography

The reference material consulted while preparing this section. Microsoft’s documentation is the primary source; everything here targets C# 14 on .NET 10 unless stated otherwise.

Microsoft Learn — C# documentation

  • C# documentation hub — the entry point for everything below.

  • A tour of C# — the guided introduction to the language and the .NET architecture.

  • C# fundamentals — types, object-oriented programming, functional techniques, exceptions and the coding style guidance.

  • C# programming guide — the task-oriented companion to the fundamentals.

  • LINQ in C# — query expressions, the standard query operators and LINQ to Objects.

  • Asynchronous programming in C# — async/await, the task asynchronous programming model and async streams.

  • Advanced topics — reflection and attributes, interop, expression trees, performance and unsafe code.

Microsoft Learn — language reference and what’s new

The specification and the standard

Microsoft Learn — .NET documentation

Releases and support

  • .NET support policy — the LTS and STS cadence that makes .NET 10 supported into 2028.

  • .NET downloads — the SDKs and runtimes, including the previews the C# 15 material refers to.

Testing frameworks

  • xUnit.net and NUnit — consulted for the Testing page only; every other page links Microsoft documentation exclusively.

Books consulted

  • Albahari, Joseph. C# 12 in a Nutshell: The Definitive Reference. O’Reilly Media, December 2023. ISBN 978-1-098-14744-0. Consulted as a reference while preparing these pages — see the publisher’s book page and the author’s companion site at albahari.com/nutshell.

  • Skeet, Jon. C# in Depth, 1st ed. Manning Publications, 2008. ISBN 1-933988-36-3. Consulted as a reference while preparing these pages — see the publisher’s book page, which now lists the fourth edition, and the author’s companion site at csharpindepth.com.

The books above are consulted references, not the primary source for this section; on any discrepancy, or wherever they are silent on a feature introduced after they were written, the official Microsoft documentation and the ECMA-334 standard win.