C# Cheat Sheet
Free C# cheat sheet: syntax, LINQ, async/await, properties, and OOP essentials in one quick reference.
Basics
| Concept | Syntax | Example |
|---|---|---|
| Variable | var x = 5; int y = 10; | var name = "Alice"; |
| String interpolation | $"text {expr}" | $"Hello {name}, age {age}" |
| Null coalescing | a ?? b | var result = input ?? "default"; |
| Pattern matching | is pattern | if (obj is string s) Console.Write(s); |
Learn it in a lesson: Variables & Data Types · Nullable Types & Null Operators · Pattern Matching & Switch Expressions
LINQ
| Concept | Syntax | Example |
|---|---|---|
| Filter | .Where(x => ...) | var adults = people.Where(p => p.Age >= 18); |
| Project | .Select(x => ...) | var names = people.Select(p => p.Name); |
| Sort | .OrderBy / .ThenBy | people.OrderBy(p => p.Last).ThenBy(p => p.First); |
| One item First throws on empty; FirstOrDefault returns null. Single throws if there is more than one. | .First / .FirstOrDefault / .Single | var ada = people.FirstOrDefault(p => p.Name == "Ada"); |
| Aggregate | .Count() .Sum() .Average() .Max() | var total = orders.Sum(o => o.Amount); |
| Group | .GroupBy(x => key) | orders.GroupBy(o => o.Country); |
| Run it now LINQ is lazy — nothing runs until you enumerate it. | .ToList() / .ToArray() | var list = query.ToList(); |
Learn it in a lesson: LINQ
OOP
| Concept | Syntax | Example |
|---|---|---|
| Class | class Name { } | class Dog { public string Name { get; set; } } |
| Inheritance | class B : A { } | class Puppy : Dog { } |
| Interface | interface IName { } | interface IDrawable { void Draw(); } |
| Record | record Name(params) | record Person(string Name, int Age); |
Learn it in a lesson: Object-Oriented Programming · Inheritance & Polymorphism · Records
Collections
| Concept | Syntax | Example |
|---|---|---|
| List | List<T> | var names = new List<string> { "Ada" }; names.Add("Alan"); |
| Dictionary | Dictionary<K,V> | var ages = new Dictionary<string,int> { ["Ada"] = 36 }; |
| HashSet | HashSet<T> | var set = new HashSet<int> { 1, 2, 3 }; |
| Safe lookup Indexing a missing key throws; TryGetValue returns false instead. | TryGetValue(key, out var v) | if (ages.TryGetValue("Ada", out var a)) { } |
| Immutable view Hands callers the data without handing them Add and Remove. | IReadOnlyList<T> | public IReadOnlyList<Item> Items => _items; |
Learn it in a lesson: Collections · Generic Lists (List<T>) · Dictionary (Key-Value Pairs) · Domain-Driven Design (Aggregates, Value Objects, Repositories)
Async
| Concept | Syntax | Example |
|---|---|---|
| Async method | async Task<T> Method() | async Task<string> GetAsync() { ... } |
| Await | await asyncMethod() | var data = await GetDataAsync(); |
| Task.WhenAll | await Task.WhenAll(t1, t2) | await Task.WhenAll(task1, task2); |
Learn it in a lesson: Async & Await
Null Safety
| Concept | Syntax | Example |
|---|---|---|
| Nullable type With nullable reference types on, the compiler warns when you might dereference null. | string? name | string? middle = null; |
| Null-conditional Short-circuits to null instead of throwing. | obj?.Prop | var n = user?.Address?.City; |
| Null-coalescing | a ?? b | var city = user?.City ?? "Unknown"; |
| Assign if null | a ??= b | _cache ??= Build(); |
| Guard a parameter .NET 6+. Fails at the boundary with the parameter's name in the message. | ArgumentNullException.ThrowIfNull(x) | ArgumentNullException.ThrowIfNull(order); |
Learn it in a lesson: Nullable Types & Null Operators