C# Cheat Sheet

Free C# cheat sheet: syntax, LINQ, async/await, properties, and OOP essentials in one quick reference.

Basics

ConceptSyntaxExample
Variablevar x = 5; int y = 10;var name = "Alice";
String interpolation$"text {expr}"$"Hello {name}, age {age}"
Null coalescinga ?? bvar result = input ?? "default";
Pattern matchingis patternif (obj is string s) Console.Write(s);

Learn it in a lesson: Variables & Data Types · Nullable Types & Null Operators · Pattern Matching & Switch Expressions

LINQ

ConceptSyntaxExample
Filter.Where(x => ...)var adults = people.Where(p => p.Age >= 18);
Project.Select(x => ...)var names = people.Select(p => p.Name);
Sort.OrderBy / .ThenBypeople.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 / .Singlevar 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

ConceptSyntaxExample
Classclass Name { }class Dog { public string Name { get; set; } }
Inheritanceclass B : A { }class Puppy : Dog { }
Interfaceinterface IName { }interface IDrawable { void Draw(); }
Recordrecord Name(params)record Person(string Name, int Age);

Learn it in a lesson: Object-Oriented Programming · Inheritance & Polymorphism · Records

Collections

ConceptSyntaxExample
ListList<T>var names = new List<string> { "Ada" }; names.Add("Alan");
DictionaryDictionary<K,V>var ages = new Dictionary<string,int> { ["Ada"] = 36 };
HashSetHashSet<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

ConceptSyntaxExample
Async methodasync Task<T> Method()async Task<string> GetAsync() { ... }
Awaitawait asyncMethod()var data = await GetDataAsync();
Task.WhenAllawait Task.WhenAll(t1, t2)await Task.WhenAll(task1, task2);

Learn it in a lesson: Async & Await

Null Safety

ConceptSyntaxExample
Nullable type
With nullable reference types on, the compiler warns when you might dereference null.
string? namestring? middle = null;
Null-conditional
Short-circuits to null instead of throwing.
obj?.Propvar n = user?.Address?.City;
Null-coalescinga ?? bvar city = user?.City ?? "Unknown";
Assign if nulla ??= 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