All C# snippets · 32
Every snippet has its own focus page with an explanation and the output it produces.
Variables & Types · 4
- var And Explicit Typesvar infers the type at compile time; it is still statically typed. String interpolation with $ embeds expressions directly.
- Structs Copy, Classes ReferenceA struct is a value type copied on assignment. A class is a reference type, so both variables point at the same object.
- Nullable Value TypesThe ? suffix lets a value type hold null. The ?? operator supplies a fallback when the left side is null.
- const vs readonlyconst is baked in at compile time. readonly only stops the field being reassigned, so the object it points at can still be mutated.
Strings · 2
Control Flow · 2
Collections · 3
- List BasicsList<T> is the growable array. Collection initialiser syntax populates it inline without repeated Add calls.
- Dictionary And TryGetValueTryGetValue looks the key up once and reports success through the return value, avoiding a second lookup or an exception.
- Indexer Throws On Missing KeyUnlike Go or Python's get, the C# indexer throws for an absent key. Use TryGetValue or GetValueOrDefault instead.
Classes · 3
- Auto PropertiesProperties look like fields but run accessors. init allows assignment only during construction, and => defines a computed property.
- Records And withRecords give value equality and a generated ToString. The with expression copies an instance while changing named members.
- Primary ConstructorA primary constructor declares parameters on the class itself and makes them available throughout the body.
Inheritance · 3
- virtual And overrideMethods are non-virtual by default in C#. A base method must be marked virtual before a subclass may override it.
- Implementing An InterfaceInterfaces declare a contract with no state. Unlike Go, implementation is explicit through the : syntax.
- Abstract Base ClassAn abstract class cannot be instantiated and can combine abstract members with shared concrete behaviour.
LINQ · 4
- LINQ Method ChainLINQ operators are extension methods on IEnumerable. Nothing executes until a terminal call such as ToList enumerates the chain.
- Query Expression SyntaxQuery syntax reads like SQL and compiles to the same method calls. Both forms are interchangeable.
- Queries Re-run Each TimeA LINQ query holds a recipe, not a result. Enumerating it again sees later changes to the source, which surprises most people once.
- Grouping And AggregatingGroupBy produces a sequence of groups keyed by the selector, each of which is itself enumerable.
Pattern Matching · 1
Exceptions · 2
Async · 3
- Awaiting A TaskAn async method returns a Task and await releases the thread until it completes, rather than blocking it.
- Awaiting Tasks TogetherStarting the tasks before awaiting lets them overlap. Task.WhenAll waits for all of them and collects the results in order.
- Do Not Block On AsyncCalling .Result or .Wait() on a Task can deadlock in contexts with a synchronisation context. Await all the way up instead.