Language cheatsheet
C# 14 Cheatsheet
Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
Basics
- Write a program without a Main method — Top-level statements: the file's statements are the program. Implicit usings bring in common namespaces.
- Declare variables — Use var when the type is obvious. const values must be known at compile time.
- Insert values into a string — Prefix a string with $ and put expressions in braces, with an optional format.
- Write multi-line text — Raw string literals keep quotes and line breaks as written, with no escaping.
- Handle null safely — Mark possibly-null reference types with ?. The compiler warns when you dereference them.
- Assign only when null — Assign a default if a variable is currently null.
- Return several values — Tuples group values without defining a type, and can be deconstructed.
- Write a local function — Define a helper inside a method, where only that method can use it.
- Get a name as a string — Refer to variables and members without hard-coding their names.
Collections and LINQ
- Create collections concisely — Collection expressions build arrays, lists and spans with the same syntax. .. spreads another collection.
- Use a dictionary — Add and read values by key. TryGetValue avoids an exception for missing keys.
- Keep unique items — A set rejects duplicates, and Add returns false if the item was already there.
- Filter and transform — LINQ methods work on any sequence. Nothing runs until you enumerate the result.
- Sort by one or more keys — Sort by one key, then break ties with another.
- Group items — Group by a key and compute a value for each group.
- Find or test elements — FirstOrDefault returns null or default when nothing matches, while First throws.
- Total, average and extremes — Compute simple aggregates over a sequence.
- Join two sequences — Match items from two sequences by a key.
- Generate numbers — Produce a sequence of integers.
Records, classes and pattern matching
- Declare an immutable data type — A record gives you value equality, ToString and deconstruction.
- Copy a record with changes — Create a modified copy and leave the original unchanged.
- Declare a class with a primary constructor — Put constructor parameters on the type declaration and use them in members.
- Require properties at creation — Force callers to set a property, and make it settable only during initialization.
- Customize an auto-property — C# 14: use the field keyword to reach the compiler-generated backing field.
- Choose a value with a switch expression — A compact switch that returns a value, with a default.
- Test and unpack a type — Check a type and bind it to a variable in one expression.
- Match the shape of a list — Match arrays and lists by length and contents.
- Define an enum — A named set of constants, and parsing one from text.
- Add a method to an existing type — A static method in a static class, with this on its first parameter.
- Constrain a generic type — Require that a type argument has certain capabilities.
- Provide a default interface method — An interface can include an implementation that classes may override.
Async, errors and resources
- Write an async method — Await I/O without blocking a thread. Async methods return Task or Task<T>.
- Run tasks in parallel — Start several tasks and wait for all of them.
- Support cancellation — Pass a CancellationToken so callers can stop long operations, for example on a timeout.
- Handle exceptions — Catch specific exceptions, filter with when, and clean up in finally.
- Dispose a resource automatically — A using declaration disposes the object at the end of the enclosing scope.
- Read and write files — Read and write text asynchronously.
- Serialize to and from JSON — Convert objects to JSON text and back with System.Text.Json.
- Work with dates and times — DateOnly and TimeOnly model a date or a time of day on its own.
- Protect shared state — Use a lock to let only one thread run a block at a time.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.
- JavaScript 2025 cheatsheet — Modern JavaScript for arrays, strings, objects, async code, the DOM and modules, in copy-ready snippets.