Skip to content
Home
Cheat Sheets
C#
C# Cheat Sheet
Learn C# from the basics →
Variables & Types
int x = 5; double d = 3.14; bool b = true; char c = 'A';
Basic types
string name = "Ada";
String type
const int Max = 100;
Compile-time constant
var count = 10;
Local type inference
Control Flow
if (x > 0) { } else if (x == 0) { } else { }
Conditional
for (int i = 0; i < 10; i++) { }
Counting loop
foreach (var item in list) { }
Iterate a collection
var name = day switch { 1 => "Mon", _ => "?" };
Switch expression
Collections
var list = new List<int> { 1, 2, 3 };
Resizable list
var dict = new Dictionary<string, int>();
Key/value map
var set = new HashSet<string>();
Unique elements
OOP
public class Dog : Animal, IPet { }
Inheritance + interface implementation
public int Age { get; private set; }
Auto-property with a private setter
public override string ToString() => "...";
Overriding a virtual method
public record Point(int X, int Y);
Record — immutable data carrier
Exceptions & Nullability
try { Risky(); } catch (IOException e) { } finally { }
Handle a specific exception
string? name = null;
Nullable reference type
var length = name?.Length ?? 0;
Null-conditional + null-coalescing
LINQ & async
list.Where(x => x > 0).Select(x => x * 2).ToList();
Filter and transform
async Task<int> GetAsync() { return await FetchAsync(); }
Async method
Cookies & your privacy
We use essential cookies to keep this site working and secure.
With your permission, we'd also like to use Google Analytics cookies to understand site traffic.
We don't use advertising or social media tracking cookies, and we honor Global Privacy Control (GPC) browser signals.
Learn more in our Privacy Policy .
Your browser is sending a GPC signal, so analytics stays off even if you accept.
Accept
Reject