C# Reference
Free reference guide: C# Reference
About C# Reference
The C# Reference is a comprehensive, searchable cheat sheet covering the full breadth of modern C# development — from foundational syntax like variable declarations, string interpolation, and nullable types, through object-oriented features like classes, abstract classes, records, and structs, all the way to advanced topics like LINQ queries, async/await patterns, delegates, lambda expressions, and C# 8–11 pattern matching. Whether you need a reminder on how to use switch expressions, write an IAsyncEnumerable stream, or apply property patterns, every entry includes a real, runnable code example.
This reference is particularly valuable for .NET developers, full-stack engineers building ASP.NET applications, and developers working with Unity, Blazor, or MAUI. The content spans 8 categories — Basics, Classes, Interfaces, Generics, LINQ, Async, Delegates, and Pattern Matching — so you can drill directly into the topic you need without scrolling through unrelated material. Special attention is given to modern C# features: records and init-only properties from C# 9, default interface methods from C# 8, and list patterns introduced in C# 11.
Each entry follows a consistent format: the syntax or keyword, a plain-language description, and a concise code example that demonstrates real-world usage. LINQ entries cover both query syntax and method syntax, including aggregation with Sum/Average/Max, grouping with GroupBy, and joining collections. The Async section covers Task, Task.WhenAll, CancellationToken, and IAsyncEnumerable. The Delegates section covers Action/Func, custom delegates, events, and lambda expressions — giving you a full picture of functional patterns in C#.
Key Features
- Covers 8 categories: Basics, Classes, Interfaces, Generics, LINQ, Async, Delegates, Pattern Matching
- LINQ section includes both query syntax and method syntax with aggregation, GroupBy, and Join examples
- Async coverage for Task, Task.WhenAll, CancellationToken, and IAsyncEnumerable (C# 8)
- Modern C# syntax: records (C# 9), init-only properties, switch expressions (C# 8), list patterns (C# 11)
- Class hierarchy features: inheritance, abstract classes, interfaces with default methods, structs, and records
- Generic classes and methods with type constraints (where T : class, IComparable<T>, new())
- Delegates, events, Action/Func built-in types, and lambda expressions with real examples
- Instant search and category filter — find any syntax in under 3 seconds
Frequently Asked Questions
What C# versions does this reference cover?
This reference covers syntax from C# 6 through C# 11, including string interpolation, nullable types, LINQ, async/await, records (C# 9), default interface methods (C# 8), switch expressions (C# 8), and list patterns (C# 11). It focuses on the features most commonly used in modern .NET development.
What is the difference between var and specific types in C#?
In C#, var uses type inference — the compiler determines the type at compile time based on the assigned value. It is equivalent to declaring the type explicitly. For example, var name = "Kim" is identical to string name = "Kim". The type is still strongly typed; var just reduces verbosity. const and readonly are for immutable values, where const is a compile-time constant and readonly is set only during construction.
How does LINQ work in C#?
LINQ (Language Integrated Query) lets you query collections using either query syntax (similar to SQL) or method syntax (chained lambda calls). For example, you can write from n in numbers where n % 2 == 0 select n, or equivalently numbers.Where(n => n % 2 == 0). LINQ supports filtering (Where), ordering (OrderBy), transformation (Select), aggregation (Sum, Average, Count, Max), grouping (GroupBy), and joining (Join).
What is the record type introduced in C# 9?
A record in C# 9 is a reference type that uses value-based equality by default — two records with the same data are considered equal. Records are immutable by default and support the with expression for non-destructive mutation (creating a copy with one field changed). They are ideal for DTOs, domain value objects, and any data container where structural equality is more useful than reference identity.
How does async/await work with CancellationToken?
CancellationToken provides a cooperative cancellation mechanism. You create a CancellationTokenSource, pass its Token to async methods, and the method periodically checks IsCancellationRequested or passes the token to awaitable calls like Task.Delay(ms, token). When you call CancellationTokenSource.Cancel(), the next await will throw a OperationCanceledException, allowing the async operation to stop cleanly.
What is the difference between abstract class and interface in C#?
An abstract class can have state (fields), constructors, and both abstract and concrete methods. A class can only inherit from one abstract class. An interface defines a contract with no state (prior to C# 8), but C# 8 added default interface method implementations. A class can implement multiple interfaces. Use abstract classes when you need shared state or constructors; use interfaces for defining capabilities that unrelated classes can share.
What are extension methods and when should I use them?
Extension methods let you add new methods to existing types without modifying or inheriting from them. They are defined as static methods in a static class, with the first parameter using the this keyword. For example, public static string Reverse(this string s) adds a Reverse() method to all strings. Use them to add utility methods to types you cannot modify (like built-in types or third-party classes), but avoid overusing them as they can make code harder to trace.
How do generics with constraints work in C#?
Generic constraints (where T : ...) restrict which types can be used as a type argument. Common constraints include where T : class (must be reference type), where T : struct (must be value type), where T : new() (must have parameterless constructor), where T : IComparable<T> (must implement IComparable), and where T : SomeBaseClass. Multiple constraints can be combined. Constraints help the compiler provide type-specific operations and improve type safety.