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); |
LINQ
| Concept | Syntax | Example |
|---|
| Where | .Where(x => condition) | list.Where(x => x > 5) |
| Select | .Select(x => transform) | list.Select(x => x.Name) |
| OrderBy | .OrderBy(x => key) | list.OrderBy(x => x.Age) |
| GroupBy | .GroupBy(x => key) | list.GroupBy(x => x.Dept) |
| Aggregate | .Sum() .Count() .Average() | list.Sum(x => x.Price) |
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); |
Collections
| Concept | Syntax | Example |
|---|
| List | List<T> | var list = new List<string> { "a", "b" }; |
| Dictionary | Dictionary<K,V> | var dict = new Dictionary<string,int>(); |
| HashSet | HashSet<T> | var set = new HashSet<int> { 1, 2, 3 }; |
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); |