C# Cheat Sheet
Core C# syntax, LINQ queries, generic collections, async/await patterns, and object-oriented features for building .NET applications.
2 PagesIntermediateApr 8, 2026
Basic Syntax
Variables, control flow, and console output.
csharp
using System;class Program { static void Main(string[] args) { int age = 30; string name = "Ada"; double pi = 3.14159; bool isFun = true; if (age >= 18) { Console.WriteLine($"{name} is an adult"); } for (int i = 0; i < 5; i++) { Console.WriteLine($"Count: {i}"); } }}
LINQ
Query and transform collections declaratively.
csharp
var nums = new List<int> { 5, 3, 1, 4, 2 };var evens = nums.Where(n => n % 2 == 0) .Select(n => n * n) .ToList(); // [16, 4]var sorted = nums.OrderBy(n => n).ToList(); // [1, 2, 3, 4, 5]int sum = nums.Sum(); // 15var first = nums.FirstOrDefault(n => n > 3); // 5
Async/Await
Non-blocking asynchronous operations.
csharp
using System.Net.Http;using System.Threading.Tasks;async Task<string> FetchDataAsync(string url) { using var client = new HttpClient(); string result = await client.GetStringAsync(url); return result;}async Task Main() { string data = await FetchDataAsync("https://api.example.com"); Console.WriteLine(data);}
Core Keywords
Common modern C# keywords.
- var- implicitly typed local variable, inferred at compile time
- readonly- field assignable only in declaration or constructor
- record- immutable reference type with value-based equality (C# 9+)
- interface- defines a contract implemented by classes/structs
- async/await- non-blocking asynchronous method execution
- nullable (?)- e.g. int? marks a value type as nullable
- using- ensures IDisposable resources are released deterministically
- LINQ- language-integrated query syntax for collections
Pattern Matching
Switch expressions and type/property patterns.
csharp
string Describe(object shape) => shape switch{ Circle { Radius: > 10 } => "big circle", Circle c => $"circle r={c.Radius}", Rectangle { Width: var w, Height: var h } when w == h => "square", null => "nothing", _ => "unknown",};// Tuple patternsstatic string Quadrant(int x, int y) => (x, y) switch{ (> 0, > 0) => "I", (< 0, > 0) => "II", _ => "axis or other",};
Records & With Expressions
Immutable value types with non-destructive mutation.
csharp
public record Person(string Name, int Age);var a = new Person("Ada", 36);var b = a with { Age = 37 }; // copy, change one fieldConsole.WriteLine(a == b); // False (value equality)Console.WriteLine(a); // Person { Name = Ada, Age = 36 }// record struct for a value-type recordpublic record struct Point(int X, int Y);
Nullable Reference Types
Compiler-tracked nullability and null-handling operators.
csharp
#nullable enablestring? maybe = GetName();// null-coalescing and null-conditionalint len = maybe?.Length ?? 0;// null-coalescing assignmentmaybe ??= "default";// null-forgiving operator: assert non-null to the compilerProcess(maybe!);void Process(string s) => Console.WriteLine(s.ToUpper());
Collection Types
Common generic collections in System.Collections.Generic.
- List<T>- dynamic array with index access and Add/Remove
- Dictionary<K,V>- hash map of unique keys to values
- HashSet<T>- unordered set of unique elements
- Queue<T>- FIFO collection with Enqueue/Dequeue
- Stack<T>- LIFO collection with Push/Pop
- IEnumerable<T>- lazily-iterable sequence; base of LINQ
- ImmutableList<T>- thread-safe collection that returns a new copy on edit
Pro Tip
Use 'using' declarations (var file = new StreamReader(...);) instead of using blocks in C# 8+ for automatic disposal at the end of scope.
Was this cheat sheet helpful?
Explore Topics
#CCheatSheet#Programming#Intermediate#BasicSyntax#LINQ#AsyncAwait#CoreKeywords#OOP#DataStructures#Databases#Concurrency#CheatSheet#SkillVeris