C# Cheat Sheet
This C# cheat sheet covers twenty-four sections, from Hello World to LINQ, records, pattern matching, async and ASP.NET Core. Search it and copy any line with one click. Covers C# 8 through C# 14 with the version noted on every modern feature.
By Bogdan SanduUpdated October 2, 2026
Checked against the C# 14 and .NET 10 documentation
Getting Started
Hello World
Top level statements. No class, no Main, one file.
Console.WriteLine("Hello, World!");Hello, World!The classic full form, still valid everywhere
namespace HelloApp;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}Async entry point, returns an exit code
// top level statements: await and return work directly in Program.cs
await Task.Delay(100);
return 0;
// the same inside an explicit class
static async Task<int> Main(string[] args)
{
await Task.Delay(100);
return 0;
}Console Input and Output
Write with and without a line break
Console.Write("no newline ");
Console.WriteLine("with newline");Read a line, then parse it safely
Console.Write("Age: ");
string? input = Console.ReadLine();
if (int.TryParse(input, out int age))
Console.WriteLine($"Next year: {age + 1}");Read a single key without waiting for Enter
ConsoleKeyInfo key = Console.ReadKey(intercept: true);
Console.WriteLine($"You pressed {key.Key}");Command line arguments
// top level statements expose args automatically
foreach (string arg in args)
Console.WriteLine(arg);Comments and Regions
Single line, block and XML documentation
// single line comment
/* block comment
across lines */
/// <summary>Adds two numbers.</summary>
/// <param name="a">First value.</param>
/// <returns>The sum.</returns>
int Add(int a, int b) => a + b;Collapsible region, useful in generated files
#region Helpers
// folded in the IDE
#endregionWhere C# Runs
One language, and the template that starts each kind of project
| Build | Stack | Start with |
|---|---|---|
| Console tool | .NET | dotnet new console |
| Web API | ASP.NET Core | dotnet new webapi |
| Server rendered site | ASP.NET Core MVC | dotnet new mvc |
| Web UI in C# | Blazor | dotnet new blazor |
| Cross platform app | .NET MAUI | dotnet new maui |
| Windows desktop | WPF or WinForms | dotnet new wpf |
| Games | Unity or Godot | the Unity Hub, C# is the scripting language |
| Background service | Generic Host | dotnet new worker |
| Reusable library | .NET | dotnet new classlib |
| Tests | xUnit | dotnet new xunit |
The base class library is the same in every .NET app
Console apps, web APIs, Blazor and MAUI apps all run on the same .NET runtime and base class library, so LINQ, the collections and async behave identically in each. What changes is the hosting model and the UI layer. Unity is the exception: it runs C# on its own Mono or IL2CPP runtime and supports an older language version, so the newest syntax on this page may not compile there yet.
// this compiles and runs unchanged in a console app,
// a web API, a Blazor app and a MAUI app
var recent = orders
.Where(o => o.PlacedAt > cutoff)
.OrderByDescending(o => o.Total)
.Take(10)
.ToList();Namespaces and Usings
File scoped namespace, one less level of indentation
namespace MyApp.Services;
public class OrderService { }Using directives, aliases and static imports
using System.Text;
using Json = System.Text.Json.JsonSerializer;
using static System.Math;
double r = Sqrt(16); // no Math. prefix
string s = Json.Serialize(r);Global using, applies to every file in the project
// GlobalUsings.cs
global using System.Collections.Generic;
global using System.Linq;Alias any type, including generics and arrays
using Matrix = double[,];
using Pair = (int Id, string Name); // C# 12C# Types & Variables
Declaring Variables
Explicit type versus inferred type
int count = 42;
string name = "Ana";
var total = 19.99m; // inferred as decimal
var items = new List<int>();Constants and read only fields
const double Pi = 3.14159; // compile time
static readonly DateTime Start = DateTime.Now; // run time, set onceDefault values and discards
int zero = default; // 0
string? nothing = default; // null
_ = ComputeAndIgnore(); // discard the resultBuilt in Value Types
Size, range and the alias to .NET type mapping
| Keyword | .NET type | Size | Range or precision |
|---|---|---|---|
| byte | System.Byte | 8 bit | 0 to 255 |
| sbyte | System.SByte | 8 bit | -128 to 127 |
| short | System.Int16 | 16 bit | -32,768 to 32,767 |
| ushort | System.UInt16 | 16 bit | 0 to 65,535 |
| int | System.Int32 | 32 bit | -2.1B to 2.1B |
| uint | System.UInt32 | 32 bit | 0 to 4.2B |
| long | System.Int64 | 64 bit | -9.2E18 to 9.2E18 |
| ulong | System.UInt64 | 64 bit | 0 to 1.8E19 |
| float | System.Single | 32 bit | about 7 digits, suffix f |
| double | System.Double | 64 bit | about 15 digits, default |
| decimal | System.Decimal | 128 bit | 28 digits, money, suffix m |
| bool | System.Boolean | 8 bit | true or false |
| char | System.Char | 16 bit | a single UTF-16 unit |
| nint / nuint | System.IntPtr / System.UIntPtr | 32 or 64 bit | native sized integer, follows the process |
Literal suffixes and digit separators
long big = 9_000_000_000L;
float f = 1.5f;
decimal money = 19.99m;
uint u = 42U;
int hex = 0xFF; // 255
int binary = 0b1010_1010; // 170Min and max values
Console.WriteLine(int.MaxValue); // 2147483647
Console.WriteLine(double.Epsilon);
Console.WriteLine(decimal.MinValue);Nullability
Nullable value type, a struct that can hold null
int? maybe = null;
if (maybe.HasValue)
Console.WriteLine(maybe.Value);
int safe = maybe ?? 0; // fallback
int forced = maybe.Value; // throws InvalidOperationException when nullNullable reference types, a compiler contract not a runtime one
Nothing is checked when the program runs. The annotations only drive compiler warnings, so a value arriving from JSON, a database or a library compiled without the feature can still be null despite being typed as non nullable. Treat the warnings as a strong hint and keep validating at your boundaries.
#nullable enable
string notNull = "always set";
string? mayBeNull = null;
int len = mayBeNull?.Length ?? 0; // no exceptionEnable it project wide in the csproj
<Nullable>enable</Nullable>Null guards, one line each
ArgumentNullException.ThrowIfNull(order);
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ObjectDisposedException.ThrowIf(_disposed, this);Casting and Conversion
Implicit widening versus explicit narrowing
int i = 100;
long l = i; // implicit, no data loss
double d = 9.87;
int truncated = (int)d; // explicit, becomes 9Parsing strings the safe way
int n = int.Parse("42"); // throws on bad input
bool ok = int.TryParse("7", out int v); // never throws
Console.WriteLine(n);
Console.WriteLine($"{ok} {v}");42
True 7Convert class handles null and rounding differently
int.Parse only accepts text and throws on null. Convert.ToInt32 returns 0 for null, and for a double it rounds to the nearest whole number, sending exact halves to the even neighbour, so 8.5 becomes 8 and 9.5 becomes 10, where a cast simply drops the fraction. Two different results from what looks like the same conversion, so pick deliberately.
int a = Convert.ToInt32("42");
int b = Convert.ToInt32(9.87); // 10, rounds where a cast truncates to 9
int e = Convert.ToInt32(8.5); // 8, an exact half goes to the even neighbour
int c = Convert.ToInt32(null); // 0, no exceptionReference conversion with is and as
A direct cast throws InvalidCastException when the type is wrong, as returns null, and is with a pattern gives you a bool plus the typed variable in one step. Prefer the is pattern: it reads better and it cannot leave you holding a null you forgot to check.
object box = "text";
if (box is string s1) Console.WriteLine(s1.Length);
string? s2 = box as string; // null instead of throwing
string s3 = (string)box; // InvalidCastException if wrongBoxing and unboxing, the hidden allocation
Assigning a value type to object wraps it in a heap allocated box, and casting back copies the value out again. It is invisible in the source, so a hot loop that stores ints in an ArrayList or a non generic API allocates once per item. Generic collections such as List of int avoid it entirely, which is most of why they exist.
int value = 5;
object boxed = value; // heap allocation
int unboxed = (int)boxed; // cast backCulture aware parsing, essential for decimals
Parse uses the current culture by default, so "1,234.56" is a valid decimal in the United States and a parse failure in Germany, where the separators are swapped. Any value that crosses a wire, a file or a database should be parsed and formatted with InvariantCulture so the machine agrees with itself.
using System.Globalization;
decimal price = decimal.Parse("1,234.56", CultureInfo.InvariantCulture);
DateTime when = DateTime.ParseExact("2026-09-09", "yyyy-MM-dd",
CultureInfo.InvariantCulture);Enums
Named constants with an underlying integer
enum Status { Draft, Paid, Shipped, Closed }
Status s = Status.Shipped;
Console.WriteLine(s);
Console.WriteLine((int)s);Shipped
2Explicit values and a different backing type
enum Http : ushort
{
Ok = 200,
NotFound = 404,
ServerError = 500
}Flags enum for bitwise combinations
[Flags]
enum Access { None = 0, Read = 1, Write = 2, Delete = 4 }
var perms = Access.Read | Access.Write;
bool canWrite = perms.HasFlag(Access.Write); // trueParsing and enumerating
Status parsed = Enum.Parse<Status>("Paid");
bool ok = Enum.TryParse("Draft", out Status d);
foreach (Status v in Enum.GetValues<Status>())
Console.WriteLine(v);Tuples
Named tuple, a lightweight multi value
var person = (Name: "Ana", Age: 30);
Console.WriteLine($"{person.Name} is {person.Age}");Ana is 30Return several values without a class
(int min, int max) Range(int[] xs) => (xs.Min(), xs.Max());
var (lo, hi) = Range(new[] { 4, 9, 1 });Swap and compare in one expression
(a, b) = (b, a);
bool same = (1, "x") == (1, "x"); // true, element wiseOperators
Arithmetic and Assignment
Integer division truncates, watch the types
When both operands are integers the result is an integer, and the fractional part is discarded rather than rounded. 7 / 2 is 3, and so is (int)(7 / 2.0) written the wrong way round. Cast one operand to double or decimal before dividing, not after.
Console.WriteLine(7 / 2); // 3
Console.WriteLine(7 % 2); // 1
Console.WriteLine(7 / 2.0); // 3.53
1
3.5Compound assignment and increment
x += 5; x -= 5; x *= 2; x /= 2; x %= 3;
x++; // post increment, returns then adds
++x; // pre increment, adds then returnsOverflow checking is off by default
Arithmetic on integers wraps silently past the maximum instead of throwing, so a counter can go from int.MaxValue straight to a large negative number and no one notices. Wrap arithmetic you do not trust in checked, or set CheckForOverflowUnderflow in the project file.
int max = int.MaxValue;
unchecked { int wrap = max + 1; } // wraps to int.MinValue
checked { int boom = max + 1; } // throws OverflowException
// with constants, int.MaxValue + 1 does not even compile (CS0220)Comparison and Logical
Comparison operators return bool
int a = 3, b = 5;
Console.WriteLine($"{a == b} {a != b} {a < b} {a > b} {a <= b} {a >= b}");False True True False True FalseShort circuiting versus always evaluating
Double ampersand and double pipe stop as soon as the answer is known, which is what makes a null check followed by a member access safe. The single character versions always evaluate both sides, so they will happily dereference the null you just tested for.
if (user != null && user.IsActive) { } // right side skipped if null
bool both = Check(a) & Check(b); // both always runReference equality versus value equality
Equals is virtual, so it runs the string override and compares characters. The == operator is chosen at compile time from the static type: typed as object it compares references, typed as string it compares content. That is why the same two values can be equal one way and not the other.
object x = new string("ab".ToCharArray());
object y = "ab";
Console.WriteLine(x.Equals(y)); // True, string compares content
Console.WriteLine(ReferenceEquals(x, y)); // False, two objects
Console.WriteLine(x == y); // False, == on object compares referencesTrue
False
FalseNull Operators
Null coalescing and null coalescing assignment
string display = name ?? "anonymous";
cache ??= LoadExpensive(); // assign only when nullNull conditional member, index and invoke
int? len = customer?.Address?.Street?.Length;
string? first = list?[0];
handler?.Invoke(this, EventArgs.Empty);Null forgiving operator, silences the warning only
The exclamation mark tells the compiler to stop warning. It emits no check and does nothing at run time, so if you are wrong you get the same NullReferenceException you would have got without it, just with no warning beforehand. Use it only when you can point at the reason the value cannot be null.
string definitely = maybeNull!; // no runtime check at allConditional and Range
Ternary conditional
string label = count == 1 ? "item" : "items";Index from end and range slicing
var letters = new[] { 'a', 'b', 'c', 'd', 'e' };
Console.WriteLine(letters[^1]); // last item
Console.WriteLine(new string(letters[1..4])); // bcd
Console.WriteLine(new string(letters[^3..])); // cdee
bcd
cdeRanges are first class values
Range middle = 1..^1;
Index last = ^1;
var slice = numbers[middle];Bitwise and Shift
The full set, on integers
Console.WriteLine(12 & 10); // AND
Console.WriteLine(12 | 10); // OR
Console.WriteLine(12 ^ 10); // XOR
Console.WriteLine(~12); // NOT
Console.WriteLine(12 << 1); // left shift
Console.WriteLine(12 >> 2); // right shift8
14
6
-13
24
3Unsigned right shift fills with zeros, C# 11
int r = -8 >>> 1; // 2147483644, zero filled
int s = -8 >> 1; // -4, the sign bit is copiedType and Meta Operators
typeof, nameof and sizeof
int count = 0;
Console.WriteLine(typeof(int));
Console.WriteLine(nameof(count));
Console.WriteLine(sizeof(int));System.Int32
count
4Get the runtime type of an instance
object o = "text";
Console.WriteLine(o.GetType().Name); // Stringnameof survives renames, string literals do not
throw new ArgumentNullException(nameof(order));C# Strings & Text
String Literals
Interpolated string, the default choice
string name = "Ana";
int age = 30;
Console.WriteLine($"{name} is {age} years old");Ana is 30 years oldVerbatim string, backslashes stay literal
string path = @"C:\Users\Ana\Documents";
string quoted = @"She said ""hi""";Raw string literal, no escaping at all
string json = """
{
"name": "Ana",
"path": "C:\Users\Ana"
}
""";Raw plus interpolation, extra dollar signs raise the brace count
string body = $$"""
{ "user": "{{name}}", "css": "{ margin: 0 }" }
""";Escape sequences
"\n" newline "\t" tab
"\\" backslash "\"" double quote
"\u00e9" unicode "\0" null characterCommon String Methods
Inspecting
s.Length s.Contains("ab")
s.StartsWith("A") s.EndsWith(".cs")
s.IndexOf('x') s.LastIndexOf('x')
string.IsNullOrEmpty(s)
string.IsNullOrWhiteSpace(s)Transforming, strings are immutable so each call returns a new one
s.ToUpper() s.ToLower()
s.Trim() s.TrimStart() s.TrimEnd()
s.Replace("a", "b") s.Insert(0, "x")
s.Substring(2, 5) s.PadLeft(8, '0')Split and join
string[] parts = "a,b,c".Split(',');
Console.WriteLine(string.Join("|", parts));a|b|cSplit with options, drops empty entries and trims
var tags = " a , , b ".Split(',',
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
// ["a", "b"]Comparison that ignores case and culture
The default string comparison is culture sensitive, which means it can change behaviour on a different machine, most famously in Turkish where lowercase I is not i. For identifiers, file names, keys and anything else that is not shown to a person, pass StringComparison.Ordinal or OrdinalIgnoreCase.
bool same = string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
int order = string.Compare(a, b, StringComparison.Ordinal);Formatting
Numeric format strings
Without a culture argument these use the culture of the machine running the code, so the same N2 prints 1.234,57 in Germany and the currency symbol follows the region. Pass a culture for text that is stored, logged or compared, and leave it out only for text shown to the user.
var us = CultureInfo.GetCultureInfo("en-US");
double v = 1234.5678;
Console.WriteLine(v.ToString("N2", us)); // thousands, 2 decimals
Console.WriteLine(v.ToString("C2", us)); // currency
Console.WriteLine(0.456.ToString("P2", us)); // percent1,234.57
$1,234.57
45.60%Alignment and format inside interpolation
Console.WriteLine($"{name,-10}|{price,8:F2}");
// left aligned in 10 cols, right aligned in 8 colsDate and time patterns
DateTime now = DateTime.Now;
now.ToString("yyyy-MM-dd") // 2026-09-09
now.ToString("HH:mm:ss") // 14:05:09
now.ToString("dddd, dd MMMM yyyy")
now.ToString("O") // round trip ISO 8601Common numeric specifiers
| Code | Meaning | 1234.5678 becomes |
|---|---|---|
| C2 | currency | $1,234.57 |
| N0 | number, no decimals | 1,235 |
| F3 | fixed point | 1234.568 |
| E2 | scientific | 1.23E+003 |
| X | hexadecimal (integers) | 4D2 for 1234 |
| D5 | padded integer | 01234 |
StringBuilder and Spans
Build long strings in a loop without allocating each time
Strings are immutable, so every concatenation allocates a new one and copies both sides. A thousand iterations of plus equals allocates a thousand progressively longer strings. StringBuilder keeps one growable buffer and copies once at the end.
using System.Text;
var sb = new StringBuilder();
for (int i = 0; i < 1000; i++)
sb.AppendLine($"row {i}");
string result = sb.ToString();Slice without copying, ideal in hot paths
Substring allocates a new string every time. A ReadOnlySpan of char is a window onto the original characters, so slicing costs nothing and parsing straight from the span avoids the intermediate strings entirely. The trade off is that a span cannot be stored on a field or captured in a lambda.
ReadOnlySpan<char> span = "2026-09-09".AsSpan();
ReadOnlySpan<char> year = span[..4];
int y = int.Parse(year);Create a string in place, no intermediate buffers
string card = "4111222233334444";
string masked = string.Create(card.Length, card, (dst, src) =>
{
dst.Fill('*');
src.AsSpan(src.Length - 4).CopyTo(dst[^4..]);
});
Console.WriteLine(masked);************4444Regular Expressions
Match and capture
using System.Text.RegularExpressions;
var rx = new Regex(@"^(\d{4})-(\d{2})-(\d{2})$");
Match m = rx.Match("2026-09-09");
Console.WriteLine(m.Success);
Console.WriteLine(m.Groups[1].Value);True
2026Source generated regex, compiled at build time
A Regex created inside a method is parsed and compiled again on every call, and even a cached one pays that cost the first time it runs. GeneratedRegex emits the matching code at build time, so there is no start up cost, the generated code can be read and debugged, and an invalid pattern is reported when you build rather than when the line runs.
public partial class Validators
{
[GeneratedRegex(@"^[\w.-]+@[\w.-]+\.\w{2,}$", RegexOptions.IgnoreCase)]
public static partial Regex Email();
}
bool ok = Validators.Email().IsMatch(input);Replace with a callback
string masked = Regex.Replace(text, @"\d",
match => "*");Control Flow
Conditionals
if, else if, else
if (score >= 90)
grade = "A";
else if (score >= 80)
grade = "B";
else
grade = "C";Guard clause style, fewer nested blocks
if (order is null) return;
if (!order.IsValid) return;
Process(order);Switch
Classic switch statement, break is required
switch (day)
{
case "Sat":
case "Sun":
Console.WriteLine("Weekend");
break;
default:
Console.WriteLine("Weekday");
break;
}Switch expression, returns a value and must be exhaustive
string day = "Sat";
string kind = day switch
{
"Sat" or "Sun" => "Weekend",
_ => "Weekday"
};
Console.WriteLine(kind);WeekendRelational patterns in a switch expression
string band = temp switch
{
< 0 => "freezing",
>= 0 and < 15 => "cold",
>= 15 and < 25 => "mild",
_ => "hot"
};Loops
for, when you need the index
for (int i = 0; i < 5; i++)
Console.Write($"{i} ");0 1 2 3 4foreach, when you only need the item
foreach (var item in collection)
Console.WriteLine(item);
// with an index, .NET 9 and later
foreach (var (i, item) in collection.Index())
Console.WriteLine($"{i}: {item}");while and do while
while (queue.Count > 0)
Handle(queue.Dequeue());
do
{
input = Console.ReadLine();
} while (input != "quit");Nested loop control
foreach (var row in grid)
{
foreach (var cell in row)
{
if (cell is null) continue; // next cell
if (cell.IsStop) goto done; // break out of both
}
}
done:
Console.WriteLine("finished");Infinite loop with an explicit exit
while (true)
{
var msg = Receive();
if (msg is null) break;
Handle(msg);
}Iterators with yield
Lazy sequence, values produced on demand
An iterator method does not run when you call it. The body executes as the caller enumerates, one yield at a time, so an infinite sequence is fine and a caller that stops early never pays for the rest. It also means side effects inside the method happen during the foreach, not at the call site.
IEnumerable<int> Odds(int max)
{
for (int i = 1; i <= max; i += 2)
yield return i;
}
Console.Write(string.Join(" ", Odds(10)));1 3 5 7 9Stop the sequence early
IEnumerable<string> ReadUntilBlank(TextReader r)
{
while (true)
{
string? line = r.ReadLine();
if (string.IsNullOrWhiteSpace(line)) yield break;
yield return line;
}
}C# Pattern Matching
Type and Constant Patterns
Test and bind in one step
if (shape is Circle c)
Console.WriteLine(c.Radius);Null checks read better as patterns
if (value is null) { }
if (value is not null) { }
if (value is { }) { } // not null, any shapeSwitch over types
double Area(object shape) => shape switch
{
Circle c => Math.PI * c.R * c.R,
Rect r => r.W * r.H,
null => 0,
_ => throw new ArgumentException("unknown shape")
};Relational and Logical Patterns
and, or, not combine any two patterns
bool isDigit = ch is >= '0' and <= '9';
bool isVowel = ch is 'a' or 'e' or 'i' or 'o' or 'u';
bool notNull = obj is not null;Ranges inside a switch expression
int age = 15;
string stage = age switch
{
< 13 => "child",
>= 13 and < 20 => "teen",
_ => "adult"
};
Console.WriteLine(stage);teenProperty Patterns
Match on the shape of an object
if (order is { Status: "paid", Total: > 100 })
ApplyDiscount(order);Nested and extended property paths, C# 10
bool local = person is { Address.Country: "RO", Age: >= 18 };Bind while matching
string Describe(Order o) => o switch
{
{ Items.Count: 0 } => "empty",
{ Total: var t } when t > 1000 => $"large order {t:C}",
{ Customer.IsVip: true } => "vip order",
_ => "standard"
};List and Positional Patterns
Match the shape of a sequence, slice with two dots
int[] xs = { 1, 5, 7, 9 };
string s = xs switch
{
[] => "empty",
[var only] => $"single {only}",
[1, .., var last] => $"starts with 1, ends with {last}",
_ => "other"
};
Console.WriteLine(s);starts with 1, ends with 9Positional pattern on records and tuples
record Point(int X, int Y);
string Where(Point p) => p switch
{
(0, 0) => "origin",
(var x, 0) => $"on X at {x}",
(0, var y) => $"on Y at {y}",
_ => "somewhere"
};Tuple pattern across two inputs
string Rps(string a, string b) => (a, b) switch
{
("rock", "scissors") or ("paper", "rock") => "first wins",
(var x, var y) when x == y => "draw",
_ => "second wins"
};Methods
Declaring Methods
Standard body and expression body
public int Add(int a, int b)
{
return a + b;
}
public int Multiply(int a, int b) => a * b;
public void Log(string m) => Console.WriteLine(m);Access modifiers
| Modifier | Visible from |
|---|---|
| public | anywhere |
| private | the same type only (default for members) |
| protected | the same type and derived types |
| internal | the same assembly (default for types) |
| protected internal | same assembly or derived types |
| private protected | derived types in the same assembly |
Overloading, same name, different signature
void Print(int n) { }
void Print(string s) { }
void Print(int n, string unit) { }Parameters
Optional and named arguments
void Send(string to, string subject = "(none)", bool urgent = false) { }
Send("a@b.com");
Send("a@b.com", urgent: true); // skip the middle oneparams takes any number of arguments
Every call that passes arguments allocates a new array (a call with none reuses a cached empty array), so params in a hot path is a hidden allocation per call. An overload that takes the common number of arguments directly, or the C# 13 params ReadOnlySpan form, removes it.
int Sum(params int[] numbers) => numbers.Sum();
Sum(1, 2, 3);
Sum(); // valid, empty arrayref passes a reference, in and out narrow the intent
ref means the callee may read and write the caller's variable, out means it must assign it before returning, and in means it may only read. They are not just documentation: out lets the compiler treat the variable as definitely assigned, and in avoids copying a large struct.
void Double(ref int x) => x *= 2;
bool TryGet(string key, out string value) { value = "hit"; return true; }
double Distance(in Point a, in Point b) => 0; // read only, no copy
int n = 5;
Double(ref n);
Console.WriteLine(n);
readonly record struct Point(double X, double Y);10params over any collection type, C# 13
void Log(params ReadOnlySpan<string> lines) { }Local Functions
A helper scoped to one method, can capture locals
int Factorial(int n)
{
if (n < 0) throw new ArgumentOutOfRangeException(nameof(n));
return Compute(n);
static int Compute(int x) => x <= 1 ? 1 : x * Compute(x - 1);
}static local functions cannot capture outer variables
A local function that reads an outer variable captures it; that costs an allocation as soon as the function is turned into a delegate, and it hides a dependency on outer state. Marking it static makes any capture a compile error, so the function can only work with its own parameters.
static bool IsEven(int x) => x % 2 == 0;Extension Methods
Add methods to a type you do not own
An extension method is ordinary static method call syntax sugar, resolved at compile time. It cannot see private state, it never overrides a real member, and it is only visible where its namespace is imported, which is why a missing using shows up as CS1061 rather than as a missing method.
public static class StringExtensions
{
public static bool IsPalindrome(this string s)
{
var clean = s.ToLower().Replace(" ", "");
return clean.SequenceEqual(clean.Reverse());
}
}
Console.WriteLine("never odd or even".IsPalindrome());TrueRules: static class, static method, this on the first parameter
public static IEnumerable<T> WhereNotNull<T>(
this IEnumerable<T?> source) where T : class
=> source.Where(x => x is not null)!;Classes & Objects
A Class From Top to Bottom
Fields, properties, constructor, method
public class BankAccount
{
private decimal _balance; // field
public string Owner { get; } // read only property
public decimal Balance => _balance; // computed property
public BankAccount(string owner, decimal opening)
{
Owner = owner;
_balance = opening;
}
public void Deposit(decimal amount)
{
if (amount <= 0) throw new ArgumentException("must be positive");
_balance += amount;
}
}Creating and using instances
var acc = new BankAccount("Ana", 100m);
acc.Deposit(50m);
Console.WriteLine(acc.Balance); // 150Properties
Auto property, computed property and full property
public string Name { get; set; } = "";
public string Upper => Name.ToUpper();
private int _age;
public int Age
{
get => _age;
set => _age = value >= 0 ? value : throw new ArgumentException();
}Access levels, init only and required
init lets a caller set the property in an object initializer and then freezes it, which gives you immutability without a long constructor. required makes the compiler enforce that the caller supplied it. Together they replace most builder patterns.
public string Id { get; private set; } // settable inside the class
public DateTime Created { get; init; } // only during construction
public required string Email { get; init; } // caller must supply itObject and collection initializers
var user = new User
{
Name = "Ana",
Email = "ana@example.com",
Tags = { "admin", "beta" }
};The field keyword removes the backing field boilerplate, final in C# 14
public string Slug
{
get => field;
set => field = value.Trim().ToLower();
}Constructors
Chaining with this and calling a base constructor
public Order() : this(Guid.NewGuid()) { }
public Order(Guid id) { Id = id; }
public PaidOrder(Guid id) : base(id) { }Primary constructor on a class, parameters are in scope everywhere
public class Greeter(string name, ILogger logger)
{
public string Hello() => $"Hi {name}";
public void Warn() => logger.LogWarning("from {Name}", name);
}Static constructor runs once, before first use
The runtime guarantees it runs exactly once, thread safely, before the first instance is created or the first static member is touched. You cannot control when, and an exception thrown inside it becomes a TypeInitializationException that is awkward to debug, so keep it trivial.
static Config()
{
Defaults = Load();
}Static, Indexers, Deconstruction
Static members belong to the type, not an instance
public static class MathHelpers
{
public static int Counter;
public static int Square(int x) => x * x;
}
MathHelpers.Square(4);Indexer, make your type usable with square brackets
public class Grid
{
private readonly int[,] _cells = new int[8, 8];
public int this[int row, int col]
{
get => _cells[row, col];
set => _cells[row, col] = value;
}
}
var g = new Grid();
g[0, 0] = 1;Deconstruct, so your type supports tuple assignment
public void Deconstruct(out string name, out int age)
{
name = Name;
age = Age;
}
var (n, a) = person;Partial class, split one type across files
// Order.cs
public partial class Order { public Guid Id { get; set; } }
// Order.Validation.cs
public partial class Order { public bool IsValid() => Id != Guid.Empty; }Records & Structs
Records
One line gives you equality, ToString and deconstruction
The compiler generates Equals, GetHashCode, ToString, a copy constructor, a Deconstruct method and the with support. Equality compares every positional member, and it also compares the runtime type, so a Dog is never equal to an Animal with the same values.
public record Person(string FirstName, string LastName);
var a = new Person("Ana", "Pop");
var b = new Person("Ana", "Pop");
Console.WriteLine(a == b);
Console.WriteLine(a);True
Person { FirstName = Ana, LastName = Pop }with expression makes a modified copy
It calls the generated copy constructor and then applies your changes, so the original is untouched. That is what makes records safe to share across threads and to cache, and it is why they suit messages, events and DTOs better than a mutable class.
var married = a with { LastName = "Ionescu" };
// a is untouchedAdd members like any class
public record Order(Guid Id, decimal Total)
{
public bool IsLarge => Total > 1000m;
public static Order Empty { get; } = new(Guid.Empty, 0m);
}Record struct, value type with value equality
public readonly record struct Money(decimal Amount, string Currency);Records inherit, and equality includes the runtime type
public record Animal(string Name);
public record Dog(string Name, string Breed) : Animal(Name);Structs
A value type, copied on assignment
Passing a struct to a method, storing it in a list or assigning it to another variable copies every field. Mutating what you think is the original then silently edits a copy, which is why a mutable struct is a well known trap. Make structs readonly and keep them small.
public readonly struct Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y) => (X, Y) = (x, y);
public override string ToString() => $"({X}, {Y})";
}ref struct lives on the stack only, Span is one
public ref struct Buffer
{
private Span<byte> _data; // cannot be boxed or captured
}When to reach for a struct
// small (roughly 16 bytes or less)
// immutable
// short lived and allocated in large numbers
// otherwise use a class or a recordClass vs Struct vs Record
Pick by semantics, not by performance folklore
| Trait | class | struct | record | record struct |
|---|---|---|---|---|
| Category | reference | value | reference | value |
| Equality | reference | field wise | value based | value based |
| Can be null | yes | only with ? | yes | only with ? |
| Inheritance | yes | no | yes | no |
| with expression | no | no | yes | yes |
| Generated ToString | no | no | yes | yes |
| Typical use | services, entities | coordinates, money | DTOs, messages | small values |
Inheritance & Interfaces
Inheritance
virtual in the base, override in the child
public class Animal
{
public virtual string Speak() => "...";
}
public class Dog : Animal
{
public override string Speak() => "Woof";
}
Animal a = new Dog();
Console.WriteLine(a.Speak());WoofCall the base implementation
public override string Speak() => base.Speak() + " Woof";abstract cannot be instantiated, sealed cannot be extended
public abstract class Shape
{
public abstract double Area(); // no body, must be overridden
public virtual string Describe() => $"area {Area():F2}";
}
public sealed class Circle : Shape
{
public double R { get; init; }
public override double Area() => Math.PI * R * R;
}new hides instead of overriding, usually a mistake
Hiding creates a second unrelated method with the same name. Which one runs depends on the compile time type of the reference, not the object, so the base version runs through a base reference and the derived one through a derived reference. That is almost never what anyone wants.
public new string Speak() => "hidden"; // base reference calls the base versionInterfaces
A contract, a type can implement many
public interface IRepository<T>
{
Task<T?> GetAsync(Guid id);
Task SaveAsync(T item);
}
public class OrderRepo : IRepository<Order>, IDisposable
{
public Task<Order?> GetAsync(Guid id) => Task.FromResult<Order?>(null);
public Task SaveAsync(Order item) => Task.CompletedTask;
public void Dispose() { }
}Default implementation, so adding a member is not a breaking change
Before C# 8, adding a method to a published interface broke every implementer. A default body means existing types keep compiling. The member is only callable through the interface type, and it cannot touch state, because interfaces still have no fields.
public interface ILogger
{
void Log(string message);
void LogError(string message) => Log($"ERROR: {message}");
}Explicit implementation hides the member from the class surface
The member exists only when the object is viewed as that interface. It is how a type implements two interfaces that declare the same signature with different meanings, and how classes keep an infrastructure member such as Dispose off their public surface.
void IDisposable.Dispose() { }
// only reachable through an IDisposable referenceStatic abstract members, generic math
public interface IAddable<T> where T : IAddable<T>
{
static abstract T Zero { get; }
static abstract T operator +(T a, T b);
}Object Members and Operators
Override ToString, Equals and GetHashCode together
Dictionary and HashSet find a candidate bucket by hash and only then call Equals. Override Equals without GetHashCode and two equal objects can land in different buckets, so a lookup for a key you just inserted fails and no exception tells you why.
public override string ToString() => $"{Name} ({Id})";
public override bool Equals(object? obj) =>
obj is User u && u.Id == Id;
public override int GetHashCode() => Id.GetHashCode();Operator overloading, == and != always come in pairs
// on a class or a plain struct; records generate == and != for you
public static Money operator +(Money a, Money b) =>
new(a.Amount + b.Amount, a.Currency);
public static bool operator ==(Money a, Money b) => a.Equals(b);
public static bool operator !=(Money a, Money b) => !a.Equals(b);Custom conversions
public static implicit operator decimal(Money m) => m.Amount;
public static explicit operator Money(decimal d) => new(d, "EUR");IComparable makes your type sortable
public int CompareTo(Money? other) =>
Amount.CompareTo(other?.Amount ?? 0);C# Generics
Generic Types and Methods
One implementation, many types, no boxing
public class Box<T>
{
public T? Value { get; set; }
public bool HasValue => Value is not null;
}
var b = new Box<int> { Value = 42 };Generic method, the type is usually inferred
T FirstOr<T>(IEnumerable<T> xs, T fallback)
{
foreach (T x in xs) return x;
return fallback;
}
int n = FirstOr(numbers, -1); // T inferred as intMultiple type parameters
public class Cache<TKey, TValue> where TKey : notnull
{
private readonly Dictionary<TKey, TValue> _map = new();
}Constraints
The full list
| Constraint | Requires that T |
|---|---|
| where T : class | is a reference type |
| where T : struct | is a non nullable value type |
| where T : notnull | is never null |
| where T : new() | has a public parameterless constructor |
| where T : BaseType | derives from BaseType |
| where T : IFoo | implements IFoo |
| where T : U | derives from another parameter |
| where T : unmanaged | contains no references |
| where T : allows ref struct | can be a ref struct (C# 13) |
Combining constraints, order matters
public T Create<T>() where T : class, IEntity, new()
{
var item = new T();
item.Id = Guid.NewGuid();
return item;
}Generic math over any number type, C# 11
using System.Numerics;
T Sum<T>(IEnumerable<T> xs) where T : INumber<T>
{
T total = T.Zero;
foreach (var x in xs) total += x;
return total;
}Variance, out for producers and in for consumers
An interface that only returns T can be treated as an interface of any base type, because everything it hands you is still valid. One that only accepts T works the other way. That is why IEnumerable of string can be assigned to IEnumerable of object but List of string cannot.
IEnumerable<object> objects = new List<string>(); // covariant, out T
Action<object> printAny = o => Console.WriteLine(o);
Action<string> printText = printAny; // contravariant, in TC# Collections & Arrays
Arrays
Declaring, fixed length once created
int[] a = new int[5]; // zero filled
int[] b = { 1, 2, 3 };
int[] c = new[] { 1, 2, 3 };
int[] d = [1, 2, 3]; // collection expression, C# 12Multidimensional versus jagged
int[,] grid = new int[3, 4]; // rectangular
grid[1, 2] = 9;
int[][] rows = new int[3][]; // jagged, rows can differ
rows[0] = new int[] { 1, 2 };Useful array operations
Array.Sort(a);
Array.Reverse(a);
Array.IndexOf(a, 3);
Array.Fill(a, 7);
int[] copy = (int[])a.Clone();
Array.Copy(a, copy, a.Length);List of T
The default resizable collection
var list = new List<string> { "a", "b" };
list.Add("c");
list.Insert(0, "z");
list.Remove("z");
Console.WriteLine(list.Count);
Console.WriteLine(list[1]);3
bSearching and bulk operations
list.Contains("a");
list.IndexOf("b");
list.Find(s => s.StartsWith("a"));
list.RemoveAll(s => s.Length > 3);
list.AddRange(other);
list.Sort();
list.Clear();Preallocate the capacity when you know the size
A List grows by allocating a new array of twice the size and copying everything across. Adding ten thousand items without a capacity does that a dozen times and leaves the old arrays to the garbage collector. Passing the expected count allocates once.
var big = new List<int>(capacity: 10_000);Dictionary
Key to value lookup, average O(1)
var ages = new Dictionary<string, int>
{
["Ana"] = 1,
["Luca"] = 2
};
Console.WriteLine(ages["Ana"]);
Console.WriteLine($"{ages.TryGetValue("Luca", out int v)} {v}");1
True 2Safe add, update and remove
The indexer throws KeyNotFoundException on read and silently overwrites on write, which is the opposite of what people expect from each. TryAdd, TryGetValue and GetValueOrDefault make the intent explicit and never throw.
ages.TryAdd("Mira", 3); // false if present, never throws
ages["Ana"] = 10; // add or overwrite
ages.Remove("Luca");
ages.ContainsKey("Ana");Iterate keys, values or pairs
foreach (var (name, age) in ages)
Console.WriteLine($"{name}: {age}");
foreach (string k in ages.Keys) { }
foreach (int val in ages.Values) { }Case insensitive keys and counting patterns
var map = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
foreach (var word in words)
map[word] = map.GetValueOrDefault(word) + 1;Sets, Queues and Stacks
HashSet holds unique values
var set = new HashSet<int> { 1, 2, 2 };
set.Add(2); // false, already there
Console.WriteLine(set.Count);2Set algebra in place
a.UnionWith(b);
a.IntersectWith(b);
a.ExceptWith(b);
bool sub = a.IsSubsetOf(b);Queue is first in first out, Stack is last in first out
var q = new Queue<string>();
q.Enqueue("first"); q.Enqueue("second");
q.Dequeue(); // "first"
var st = new Stack<string>();
st.Push("bottom"); st.Push("top");
st.Pop(); // "top"PriorityQueue, .NET 6 and later
var pq = new PriorityQueue<string, int>();
pq.Enqueue("low", 5);
pq.Enqueue("urgent", 1);
pq.Dequeue(); // "urgent", lowest priority value winsWhich Collection to Use
Pick by the operation you repeat most
| Type | Lookup | Insert | Ordered | Use it for |
|---|---|---|---|---|
| T[] | O(1) by index | fixed | yes | known fixed size, hot loops |
| List<T> | O(1) by index, O(n) by value | O(1) amortised | yes | the default sequence |
| Dictionary<K,V> | O(1) by key | O(1) | no | keyed lookup |
| HashSet<T> | O(1) contains | O(1) | no | uniqueness, membership |
| SortedDictionary<K,V> | O(log n) | O(log n) | by key | ordered keyed access |
| Queue<T> | front only | O(1) | FIFO | work pipelines |
| Stack<T> | top only | O(1) | LIFO | undo, traversal |
| LinkedList<T> | O(n) | O(1) at a node | yes | frequent middle inserts |
| ImmutableArray<T> | O(1) by index | copies | yes | shared read only data |
| ConcurrentDictionary<K,V> | O(1) | O(1) | no | multi threaded access |
Collection Expressions
One syntax for arrays, lists and spans
int[] a = [1, 2, 3];
List<int> b = [1, 2, 3];
Span<int> c = [1, 2, 3];
ImmutableArray<int> d = [1, 2, 3];Spread another collection with two dots
int[] head = [1, 2];
int[] tail = [4, 5];
int[] all = [..head, 3, ..tail];
Console.Write(string.Join(" ", all));1 2 3 4 5Immutable and Concurrent
Immutable collections return a new instance on change
Add does not modify the collection, it returns a new one that shares most of its internal structure with the old. Ignoring the return value is the classic mistake, and it looks like the call did nothing. In exchange the collection is free to share across threads with no locking.
using System.Collections.Immutable;
var list = ImmutableList.Create(1, 2, 3);
var bigger = list.Add(4); // list still has 3 itemsThread safe collections, no external lock needed
using System.Collections.Concurrent;
var cache = new ConcurrentDictionary<string, int>();
cache.AddOrUpdate("hits", 1, (_, old) => old + 1);
int v = cache.GetOrAdd("misses", _ => 0);
var bag = new ConcurrentBag<string>();
var queue = new BlockingCollection<string>();Read only views, cheap and safe to hand out
var numbers = new List<int> { 1, 2, 3 };
IReadOnlyList<int> view = numbers.AsReadOnly();
var prices = new Dictionary<string, decimal>();
ReadOnlyDictionary<string, decimal> ro = prices.AsReadOnly(); // .NET 7+LINQ Queries
Two Syntaxes, One Result
Method syntax is the one you will see most
var names = people
.Where(p => p.Age >= 18)
.OrderBy(p => p.LastName)
.Select(p => p.FirstName)
.ToList();Query syntax reads well for joins and groups
var names = from p in people
where p.Age >= 18
orderby p.LastName
select p.FirstName;Queries are lazy until you materialise them
The operators build a query object and nothing executes until you enumerate it. Enumerating twice runs the work twice, and if the underlying data changed in between you get different answers. Call ToList once when you need the results more than once.
var q = numbers.Where(n => n > 10); // nothing has run yet
var list = q.ToList(); // now it runs
// ToArray, ToList, ToDictionary, ToHashSet, Count, First all force executionFiltering and Projection
Where filters, Select transforms
int[] xs = [1, 2, 3, 4, 5, 6];
var evenSquares = xs.Where(x => x % 2 == 0).Select(x => x * x);
Console.Write(string.Join(" ", evenSquares));4 16 36SelectMany flattens nested sequences
var allTags = posts.SelectMany(p => p.Tags).Distinct();Project into an anonymous type or a record
var rows = orders.Select(o => new { o.Id, o.Total, Year = o.Date.Year });
var dtos = orders.Select(o => new OrderDto(o.Id, o.Total));Where with the index
var everyOther = xs.Where((x, i) => i % 2 == 0);Ordering, Paging, Sets
Sort by one key then another
var sorted = people
.OrderBy(p => p.LastName)
.ThenByDescending(p => p.Age);Paging with Skip and Take
int page = 3, size = 20;
var slice = query.Skip((page - 1) * size).Take(size);Set operations across sequences
a.Distinct() a.Union(b)
a.Intersect(b) a.Except(b)
a.Concat(b) a.SequenceEqual(b)Key based variants, .NET 6 and later
var unique = people.DistinctBy(p => p.Email);
var top = people.MaxBy(p => p.Score);
var chunks = people.Chunk(100); // batches of 100Grouping and Joining
GroupBy returns a sequence of groups with a Key
var byCity = people.GroupBy(p => p.City);
foreach (var group in byCity)
Console.WriteLine($"{group.Key}: {group.Count()}");Group and aggregate in one pass
var stats = orders
.GroupBy(o => o.CustomerId)
.Select(g => new
{
Customer = g.Key,
Orders = g.Count(),
Revenue = g.Sum(o => o.Total)
})
.OrderByDescending(x => x.Revenue);Inner join on a shared key
var rows = orders.Join(
customers,
o => o.CustomerId,
c => c.Id,
(o, c) => new { c.Name, o.Total });Left join through GroupJoin and DefaultIfEmpty
var left = from c in customers
join o in orders on c.Id equals o.CustomerId into g
from o in g.DefaultIfEmpty()
select new { c.Name, Total = o?.Total ?? 0 };ToLookup builds a reusable one to many index
GroupBy is lazy and re-runs on each enumeration, while ToLookup executes immediately and hands back a finished index you can query many times. A missing key returns an empty sequence rather than throwing, unlike a Dictionary.
var index = orders.ToLookup(o => o.CustomerId);
var mine = index[customerId]; // empty sequence if the key is missingAggregation and Element Access
The common aggregates
int[] xs = [1, 2, 3, 4, 5, 6];
Console.WriteLine(xs.Sum());
Console.WriteLine(xs.Average());
Console.WriteLine(xs.Max());
Console.WriteLine(xs.Min());21
3.5
6
1First, Single and their safe variants
First throws on an empty sequence, Single also throws when there is more than one match, and the OrDefault versions return null or zero instead. Single is a real assertion about your data, so use it when a second match means something is wrong, not as a habit.
xs.First(); // throws if empty
xs.FirstOrDefault(); // 0 or null
xs.FirstOrDefault(-1); // custom default, .NET 6+
xs.Single(x => x > 5); // throws if not exactly one match
xs.SingleOrDefault();
xs.Last(); xs.ElementAtOrDefault(2);Quantifiers and counting
Any stops at the first element. Count on a plain IEnumerable walks the entire sequence, although collections such as List answer from their stored count. On a database query Count becomes a COUNT statement while Any becomes an EXISTS, which is usually much cheaper. Write Any() when the question is whether there is at least one.
bool anyAdults = people.Any(p => p.Age >= 18);
bool allValid = orders.All(o => o.IsValid);
bool hasItems = list.Any(); // cheaper than Count() > 0
int adults = people.Count(p => p.Age >= 18);Aggregate folds a sequence into one value
string csv = names.Aggregate((acc, n) => $"{acc}, {n}");
int product = xs.Aggregate(1, (acc, x) => acc * x);Generators and pairing
Enumerable.Range(1, 10);
Enumerable.Repeat("x", 3);
Enumerable.Empty<int>();
names.Zip(scores, (n, s) => $"{n}: {s}");Delegates & Events
Lambdas
Expression lambda and statement lambda
Func<int, int> square = x => x * x;
Func<int, string> describe = x =>
{
if (x < 0) return "negative";
return x.ToString();
};
Console.WriteLine(square(5));25Explicit types, defaults and params, C# 12
var add = (int a, int b = 10) => a + b;
var log = (params string[] parts) => string.Join(" ", parts);Closures capture variables, not values
The lambda holds a reference to the variable itself, so it sees whatever the value is when the lambda finally runs, not what it was when the lambda was written. Since C# 5 a foreach loop gives every iteration its own variable, but a for loop still shares one counter across all iterations, so copy it into a local inside the body before capturing it.
int factor = 2;
Func<int, int> scale = x => x * factor;
factor = 10;
Console.WriteLine(scale(5)); // 50, not 10static lambda cannot capture, so it can never allocate a closure
A lambda that captures variables allocates a closure object each time it is created. A lambda that captures nothing is already cached by the compiler; static guarantees it stays that way, because an accidental capture added later becomes a compile error instead of a silent allocation.
Func<int, int> pure = static x => x * 2;Func, Action, Predicate
Built in delegate types, no custom declaration needed
Func<int, int, int> add = (a, b) => a + b; // last parameter is the return
Action<string> print = s => Console.WriteLine(s); // returns void
Predicate<int> isEven = n => n % 2 == 0; // returns boolPass behaviour into a method
void Retry(int times, Action work)
{
for (int i = 0; i < times; i++)
{
try { work(); return; }
catch when (i < times - 1) { }
}
}
Retry(3, () => CallApi());Custom delegate and multicast invocation
public delegate void Notify(string message);
Notify chain = m => Console.WriteLine($"log: {m}");
chain += m => File.AppendAllText("app.log", m);
chain("started"); // both run, in orderMethod group, shorter than a lambda that just forwards
names.ForEach(Console.WriteLine); // instead of x => Console.WriteLine(x)Events
Declare, raise and subscribe
public class Downloader
{
public event EventHandler<int>? ProgressChanged;
private void Report(int percent) =>
ProgressChanged?.Invoke(this, percent);
}
var d = new Downloader();
d.ProgressChanged += (sender, pct) => Console.WriteLine($"{pct}%");Custom event arguments
public class FileFoundEventArgs : EventArgs
{
public required string Path { get; init; }
}
public event EventHandler<FileFoundEventArgs>? FileFound;Always unsubscribe, otherwise the publisher keeps the subscriber alive
An event holds a strong reference to every handler, so a long lived publisher keeps every subscriber in memory until it is unsubscribed. This is the most common managed memory leak in .NET, and it usually shows up as controls or view models that are never collected.
d.ProgressChanged -= Handler;C# Async & Concurrency
async and await
Await releases the thread while the work is pending
private static readonly HttpClient http = new();
public async Task<string> GetTitleAsync(string url)
{
string html = await http.GetStringAsync(url);
return html[..Math.Min(50, html.Length)];
}Return types
| Signature | Use when |
|---|---|
| async Task | no result to return |
| async Task<T> | a result of type T |
| async ValueTask<T> | hot path that often completes synchronously |
| async void | event handlers only, exceptions cannot be caught |
| async IAsyncEnumerable<T> | streaming results as they arrive |
Async all the way down, never block with .Result or .Wait()
In a context that captures a synchronisation context, such as WPF or classic ASP.NET, the continuation needs the thread that is currently blocked waiting for the task, so neither can proceed and the app hangs. Even where it cannot deadlock, blocking ties up a pool thread that could be serving other work.
// wrong, can deadlock
string s = GetTitleAsync(url).Result;
// right
string s = await GetTitleAsync(url);Already finished results, without an await
Task.CompletedTask;
Task.FromResult(42);
ValueTask.FromResult(42);
Task.FromException(new IOException());Running Work in Parallel
Start together, then await together
An async method starts running when you call it, not when you await it. Awaiting the first call before making the second turns two 200 millisecond requests into 400 milliseconds. Start both, then await, or hand the whole set to Task.WhenAll.
Task<string> a = GetAsync(url1);
Task<string> b = GetAsync(url2);
string[] both = await Task.WhenAll(a, b);First one to finish wins
Task done = await Task.WhenAny(primary, fallback);
string result = await (Task<string>)done;Fan out over a collection
var results = await Task.WhenAll(urls.Select(GetAsync));Bounded concurrency, .NET 6 and later
await Parallel.ForEachAsync(urls,
new ParallelOptions { MaxDegreeOfParallelism = 4 },
async (url, ct) => await DownloadAsync(url, ct));CPU bound work belongs on a background thread
int result = await Task.Run(() => HeavyComputation());Cancellation and Timeouts
Pass the token down the whole call chain
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
try
{
await LongRunningAsync(cts.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("timed out");
}Check the token inside your own loops
foreach (var item in items)
{
ct.ThrowIfCancellationRequested();
await ProcessAsync(item, ct);
}Combine several tokens into one
using var linked = CancellationTokenSource
.CreateLinkedTokenSource(userToken, shutdownToken);Add a timeout to any task, .NET 8 and later
await slowTask.WaitAsync(TimeSpan.FromSeconds(2));Async Streams and Synchronisation
Yield results as they arrive
async IAsyncEnumerable<string> ReadLinesAsync(
string path, [EnumeratorCancellation] CancellationToken ct = default)
{
using var reader = new StreamReader(path);
while (await reader.ReadLineAsync(ct) is { } line)
yield return line;
}
await foreach (string line in ReadLinesAsync("log.txt"))
Console.WriteLine(line);lock protects shared state on synchronous paths
A lock is held by a thread, but an await can resume on a different one, so the compiler will not let you await inside a lock and the lock could not be released correctly anyway. When the critical section contains asynchronous work, use SemaphoreSlim with WaitAsync.
private readonly object _gate = new();
lock (_gate)
{
_counter++;
}
// never await inside a lockSemaphoreSlim is the async friendly gate
private readonly SemaphoreSlim _slots = new(1, 1);
await _slots.WaitAsync(ct);
try { await MutateAsync(); }
finally { _slots.Release(); }Atomic counters without a lock
Interlocked.Increment(ref _count);
Interlocked.Exchange(ref _flag, 1);
Interlocked.CompareExchange(ref _state, newValue, expected);ConfigureAwait(false) in library code, skips the captured context
By default a continuation resumes on the context it started in, which is the UI thread in a desktop app. Library code rarely needs that, and forcing every continuation back onto one thread is both slower and the mechanism behind the .Result deadlock. ASP.NET Core has no synchronisation context, so it matters less there.
var data = await client.GetAsync(url).ConfigureAwait(false);Exceptions & File I/O
try, catch, finally
Catch the specific type first, the general one last
try
{
string text = File.ReadAllText(path);
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"missing: {ex.FileName}");
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("always runs");
}Exception filter with when, the stack is not unwound if it is false
A filter runs before the stack unwinds, so if it returns false the exception carries on with its original stack intact and a debugger still breaks at the real throw site. Catching and rethrowing to achieve the same thing loses that.
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
return null;
}Rethrow, keep the original stack trace
throw with no operand rethrows the exception as it is. throw ex restarts it from the current line, which erases everything above it, so the log points at your catch block rather than at the line that actually failed.
catch (Exception ex)
{
Log(ex);
throw; // correct
// throw ex; // wrong, resets the stack trace
}Wrap with context, keep the inner exception
throw new InvalidOperationException("Could not import the order", ex);Throwing and Custom Exceptions
Common built in exceptions
| Type | Throw it when |
|---|---|
| ArgumentNullException | a required argument is null |
| ArgumentException | an argument is present but invalid |
| ArgumentOutOfRangeException | a value is outside the allowed range |
| InvalidOperationException | the object state forbids the call |
| NotSupportedException | the operation will never be supported |
| KeyNotFoundException | a dictionary key is missing |
| OperationCanceledException | a token was cancelled |
| TimeoutException | an operation exceeded its budget |
Your own exception type
public class OrderNotFoundException : Exception
{
public Guid OrderId { get; }
public OrderNotFoundException(Guid id)
: base($"Order {id} was not found")
=> OrderId = id;
}throw as an expression
string name = input ?? throw new ArgumentNullException(nameof(input));
int code = status switch { "ok" => 200, _ => throw new ArgumentException() };using and IDisposable
Deterministic cleanup, even when an exception is thrown
using compiles to try and finally, so Dispose runs on every path out of the block, including an exception or an early return. The garbage collector will not close a file handle or a database connection for you in any predictable timeframe, which is why unmanaged resources need this.
using (var stream = File.OpenRead(path))
{
// stream.Dispose() runs at the closing brace
}
// declaration form, disposed at the end of the scope
using var reader = new StreamReader(path);Async disposal
await using var conn = new SqlConnection(cs);
await conn.OpenAsync(ct);Implement it on your own type
public sealed class Session : IDisposable
{
private bool _disposed;
public void Dispose()
{
if (_disposed) return;
_handle.Close();
_disposed = true;
}
}Files and Directories
Whole file helpers, sync and async
string text = File.ReadAllText(path);
string[] lines = File.ReadAllLines(path);
byte[] bytes = File.ReadAllBytes(path);
await File.WriteAllTextAsync(path, text);
await File.AppendAllTextAsync(path, "\nmore");Existence, copy, move, delete
File.Exists(path);
File.Copy(src, dst, overwrite: true);
File.Move(src, dst);
File.Delete(path);Paths, always compose them with Path
string full = Path.Combine(root, "logs", "app.log");
string ext = Path.GetExtension(full); // .log
string name = Path.GetFileNameWithoutExtension(full);
string dir = Path.GetDirectoryName(full)!;
string temp = Path.GetTempFileName();Directories and enumeration
Directory.CreateDirectory(dir);
foreach (string f in Directory.EnumerateFiles(dir, "*.cs",
SearchOption.AllDirectories))
Console.WriteLine(f);Stream a large file line by line, low memory
using var reader = new StreamReader(path);
while (await reader.ReadLineAsync() is { } line)
Process(line);JSON with System.Text.Json
Serialize and deserialize
using System.Text.Json;
var person = new Person("Ana", 30);
string json = JsonSerializer.Serialize(person);
Console.WriteLine(json);
Person? back = JsonSerializer.Deserialize<Person>(json);
Console.WriteLine(back);
record Person(string Name, int Age);{"Name":"Ana","Age":30}Options you will almost always want
var opts = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};Attributes on the model
public class Product
{
[JsonPropertyName("product_id")] public int Id { get; set; }
[JsonIgnore] public string? Internal { get; set; }
}Stream straight to and from a file
await using var fs = File.Create(path);
await JsonSerializer.SerializeAsync(fs, data, opts);Everyday APIs
Dates and Times
Prefer DateTimeOffset or the date only types
DateTime now = DateTime.Now; // local, ambiguous
DateTime utc = DateTime.UtcNow; // store this
DateTimeOffset off = DateTimeOffset.Now; // carries the offset
DateOnly day = DateOnly.FromDateTime(now);
TimeOnly clock = TimeOnly.FromDateTime(now);Arithmetic and differences
DateTime tomorrow = now.AddDays(1);
TimeSpan age = now - birthday;
Console.WriteLine(age.TotalDays);
TimeSpan half = TimeSpan.FromMinutes(30);Time zones and testable clocks
var tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Bucharest");
var local = TimeZoneInfo.ConvertTime(utc, tz);
TimeProvider clock = TimeProvider.System; // inject this, .NET 8+
DateTimeOffset nowTestable = clock.GetUtcNow();Guid, Random and Environment
Identifiers
Guid id = Guid.NewGuid();
string text = id.ToString("N"); // no dashes
bool ok = Guid.TryParse(input, out Guid parsed);
Guid v7 = Guid.CreateVersion7(); // time ordered, .NET 9Random numbers, use the shared instance
Random.Shared is a thread safe shared instance added in .NET 6. On .NET Framework, new Random() was seeded from the system clock, so several instances created in the same tick produced identical sequences; Random.Shared also saves allocating an instance per call. Use it unless you need a fixed seed for reproducible tests.
int roll = Random.Shared.Next(1, 7); // 1 to 6
double unit = Random.Shared.NextDouble(); // 0.0 up to, not including, 1.0
Random.Shared.Shuffle(items); // .NET 8
var seeded = new Random(42); // reproducible in testsCryptographically secure randomness
using System.Security.Cryptography;
int secure = RandomNumberGenerator.GetInt32(1, 7);
byte[] key = RandomNumberGenerator.GetBytes(32);
string token = Convert.ToHexString(key);Environment and process information
Environment.GetEnvironmentVariable("PATH");
Environment.CurrentDirectory;
Environment.ProcessorCount;
Environment.NewLine;
Environment.Exit(1);
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);Math and Numbers
Rounding, and the banker's rounding surprise
Console.WriteLine(Math.Round(3.5)); // 4
Console.WriteLine(Math.Round(2.5)); // 2, not 3: halves go to the even number
Console.WriteLine(Math.Floor(-2.1)); // -3
Console.WriteLine(Math.Ceiling(4.1)); // 5
Console.WriteLine(Math.Clamp(15, 0, 10)); // 104
2
-3
5
10Control the midpoint rule explicitly
Math.Round uses banker's rounding by default, so 2.5 becomes 2 and 3.5 becomes 4. It rounds to the nearest even number to avoid the upward bias you get from always rounding .5 up. If you are calculating money or matching a spec that expects 2.5 to become 3, pass MidpointRounding.AwayFromZero explicitly.
Math.Round(2.5); // 2, to even
Math.Round(2.5, MidpointRounding.AwayFromZero); // 3
Math.Round(3.14159, 2); // 3.14Common operations
Math.Abs(-7) Math.Max(a, b) Math.Min(a, b)
Math.Pow(2, 10) Math.Sqrt(144) Math.Cbrt(27)
Math.Log(e) Math.Log10(1000) Math.Sign(-4)
Math.DivRem(17, 5, out int rem)Floating point comparison
Binary floating point cannot represent 0.1 exactly, so 0.1 + 0.2 lands slightly above 0.3 and an equality test fails. Compare doubles with a tolerance, or use decimal, which stores a base ten value and is exact for money. Never store currency in a double.
// wrong
if (0.1 + 0.2 == 0.3) { } // false
// right
if (Math.Abs((0.1 + 0.2) - 0.3) < 1e-9) { }
// or use decimal for money
if (0.1m + 0.2m == 0.3m) { } // trueTiming and Diagnostics
Measure elapsed time
using System.Diagnostics;
var sw = Stopwatch.StartNew();
DoWork();
sw.Stop();
Console.WriteLine($"took {sw.ElapsedMilliseconds} ms");
// allocation free, .NET 7+
long start = Stopwatch.GetTimestamp();
TimeSpan spent = Stopwatch.GetElapsedTime(start);Debug and trace output
Debug.WriteLine("only in debug builds");
Debug.Assert(count > 0, "count must be positive");
Trace.WriteLine("in debug and release");Run an external process
var info = new ProcessStartInfo("git", "status")
{
RedirectStandardOutput = true,
UseShellExecute = false
};
using var proc = Process.Start(info)!;
string output = await proc.StandardOutput.ReadToEndAsync();
await proc.WaitForExitAsync();HttpClient
GET and deserialize in one call
using System.Net.Http.Json;
var http = new HttpClient { BaseAddress = new Uri("https://api.example.com/") };
var user = await http.GetFromJsonAsync<User>("users/1", ct);POST with a JSON body and check the status
HttpResponseMessage res = await http.PostAsJsonAsync("orders", newOrder, ct);
res.EnsureSuccessStatusCode();
var created = await res.Content.ReadFromJsonAsync<Order>(cancellationToken: ct);Never create one per request
Every new HttpClient opens its own connection pool, and disposing one leaves the socket in TIME_WAIT for a couple of minutes. Creating one per request exhausts the available ports under load, which shows up as SocketException after the app has been running for a while. Register IHttpClientFactory and inject it, or keep a single static instance for the lifetime of the app.
// in Program.cs
builder.Services.AddHttpClient<GitHubClient>(c =>
{
c.BaseAddress = new Uri("https://api.github.com/");
c.DefaultRequestHeaders.Add("User-Agent", "my-app");
});
// then inject HttpClient into GitHubClientHeaders, timeouts and cancellation
http.Timeout = TimeSpan.FromSeconds(10);
http.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var res = await http.GetAsync("slow", cts.Token);DI, Config & Logging
Registering Services
Interface to implementation, in Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<IClock, SystemClock>();
builder.Services.AddScoped<IOrderRepository, OrderRepository>();
builder.Services.AddTransient<IEmailSender, SmtpSender>();The three lifetimes
Injecting a scoped service into a singleton is the classic DI bug. The singleton is built once and captures that first scoped instance forever, so a DbContext meant to live for one request is reused across all of them, which corrupts change tracking and is not thread safe. ASP.NET Core checks for this only in the Development environment, which is why turning scope validation on everywhere is worth it.
| Lifetime | Created | Use for |
|---|---|---|
| Singleton | once per application | caches, clocks, config, HTTP clients |
| Scoped | once per request | DbContext, unit of work, per request state |
| Transient | every time it is resolved | cheap stateless helpers |
Factory registration and instance registration
builder.Services.AddSingleton<IClock>(_ => new FixedClock(DateTime.UtcNow));
builder.Services.AddSingleton(new AppInfo("api", "1.4.0"));
builder.Services.TryAddScoped<ICache, MemoryCache>(); // skip if presentCatch lifetime mistakes at startup, not in production
builder.Host.UseDefaultServiceProvider(o =>
{
o.ValidateScopes = true;
o.ValidateOnBuild = true;
});Consuming Services
Constructor injection, the default and the one to reach for
public class OrderService(
IOrderRepository repo,
ILogger<OrderService> logger)
{
public async Task<Order?> GetAsync(Guid id)
{
logger.LogInformation("Loading order {OrderId}", id);
return await repo.GetAsync(id);
}
}Resolve a scoped service inside a singleton or a background job
public class Worker(IServiceScopeFactory scopes) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken ct)
{
using IServiceScope scope = scopes.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await db.SaveChangesAsync(ct);
}
}Get versus GetRequired
var maybe = provider.GetService<IThing>(); // null if unregistered
var must = provider.GetRequiredService<IThing>(); // throws if unregistered
var many = provider.GetServices<IHandler>(); // every registrationConfiguration
appsettings.json and per environment overrides
{
"ConnectionStrings": { "Default": "Server=.;Database=Shop" },
"Smtp": { "Host": "mail.example.com", "Port": 587 }
}
// appsettings.Development.json overrides matching keysRead a value, with a section path
string cs = builder.Configuration.GetConnectionString("Default")!;
int port = builder.Configuration.GetValue<int>("Smtp:Port");
IConfigurationSection smtp = builder.Configuration.GetSection("Smtp");Bind a section to a typed class
Binding a section to a class gives you compile time names, validation and IntelliSense instead of magic strings scattered through the codebase. Inject IOptions when the value never changes, IOptionsSnapshot when it should be re-read per request, and IOptionsMonitor when you want a callback on change.
public class SmtpOptions
{
public required string Host { get; set; }
public int Port { get; set; } = 25;
}
builder.Services.Configure<SmtpOptions>(
builder.Configuration.GetSection("Smtp"));
// then inject IOptions<SmtpOptions> and read .ValueValidate options at startup
builder.Services.AddOptions<SmtpOptions>()
.Bind(builder.Configuration.GetSection("Smtp"))
.ValidateDataAnnotations()
.ValidateOnStart();Secrets stay out of source control
dotnet user-secrets init
dotnet user-secrets set "Smtp:Password" "hunter2"
# environment variables use a double underscore for nesting
export Smtp__Password=hunter2Logging
Structured logging, keep the placeholders
The placeholders in a structured log message are not string interpolation. The logging provider keeps the template and the values separately, so a sink such as Seq or Application Insights can index OrderId as a real field and let you query on it. Writing $"Loading order {id}" instead flattens everything into one opaque string and throws that away.
logger.LogInformation("Loading order {OrderId} for {Customer}", id, name);
// not this, the values become unsearchable
logger.LogInformation($"Loading order {id} for {name}");Levels, from most to least verbose
logger.LogTrace("..."); // step by step detail
logger.LogDebug("..."); // developer diagnostics
logger.LogInformation("..."); // normal flow
logger.LogWarning("..."); // unexpected but handled
logger.LogError(ex, "..."); // the operation failed
logger.LogCritical(ex, "..."); // the app cannot continueAlways pass the exception as the first argument
catch (Exception ex)
{
logger.LogError(ex, "Import failed for {File}", path);
throw;
}Scopes attach context to everything inside
using (logger.BeginScope("Order {OrderId}", id))
{
logger.LogInformation("validated"); // carries OrderId
logger.LogInformation("charged"); // carries OrderId
}Compile time logging, no boxing and no allocation
public static partial class Log
{
[LoggerMessage(Level = LogLevel.Warning,
Message = "Retry {Attempt} for {Url}")]
public static partial void Retry(ILogger logger, int attempt, string url);
}Generic Host and Background Work
A console app with DI, config and logging
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
builder.Services.AddSingleton<IClock, SystemClock>();
IHost host = builder.Build();
await host.RunAsync();A long running background service
public class Worker(ILogger<Worker> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
logger.LogInformation("tick at {Now}", DateTimeOffset.UtcNow);
await Task.Delay(TimeSpan.FromSeconds(30), ct);
}
}
}ASP.NET Core
A Minimal API
A complete working API in one file
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IOrderRepository, OrderRepository>();
var app = builder.Build();
app.MapGet("/orders/{id:guid}", async (Guid id, IOrderRepository repo) =>
await repo.GetAsync(id) is { } order
? Results.Ok(order)
: Results.NotFound());
app.MapPost("/orders", async (Order order, IOrderRepository repo) =>
{
await repo.SaveAsync(order);
return Results.Created($"/orders/{order.Id}", order);
});
app.Run();Every verb, plus route groups
app.MapGet("/health", () => "ok");
app.MapPut("/orders/{id:guid}", Update);
app.MapDelete("/orders/{id:guid}", Delete);
app.MapPatch("/orders/{id:guid}", Patch);
var orders = app.MapGroup("/orders").RequireAuthorization();
orders.MapGet("/", ListAll);Route constraints
"/users/{id:int}" "/items/{id:guid}"
"/posts/{slug:alpha}" "/page/{n:min(1)}"
"/files/{name:length(1,64)}"
"/archive/{year:int:range(1990,2100)}"
"/optional/{tag?}"Binding and Results
Where each parameter comes from
app.MapGet("/search/{id:int}", (
[FromRoute] int id,
[FromQuery] string? q,
[FromHeader(Name = "X-Tenant")] string tenant,
[FromServices] ISearchService svc,
CancellationToken ct) => svc.FindAsync(q, ct));
// simple types bind from a route value with the same name,
// otherwise from the query string; complex types bind from the JSON bodyReturning the right status code
Results.Ok(value) Results.NoContent()
Results.Created(uri, value) Results.Accepted()
Results.NotFound() Results.BadRequest(problem)
Results.Conflict() Results.Unauthorized()
Results.Forbid() Results.File(bytes, "application/pdf")
Results.Problem("Payment declined", statusCode: 402)TypedResults documents the response for OpenAPI
app.MapGet("/orders/{id:guid}",
async Task<Results<Ok<Order>, NotFound>> (Guid id, IOrderRepository repo)
=> await repo.GetAsync(id) is { } o
? TypedResults.Ok(o)
: TypedResults.NotFound());Model validation with data annotations
public record CreateOrder(
[Required, StringLength(80)] string Customer,
[Range(1, 999)] int Quantity,
[EmailAddress] string Email);Controllers
The MVC style, still the default for larger APIs
[ApiController]
[Route("api/[controller]")]
public class OrdersController(IOrderRepository repo) : ControllerBase
{
[HttpGet("{id:guid}")]
public async Task<ActionResult<Order>> Get(Guid id)
{
Order? order = await repo.GetAsync(id);
return order is null ? NotFound() : Ok(order);
}
[HttpPost]
public async Task<ActionResult<Order>> Create([FromBody] Order order)
{
await repo.SaveAsync(order);
return CreatedAtAction(nameof(Get), new { id = order.Id }, order);
}
}Wire controllers into the pipeline
builder.Services.AddControllers();
// ...
app.MapControllers();ApiController gives you automatic 400 responses
// with [ApiController], invalid ModelState returns
// a ProblemDetails 400 before your action runs.
// to turn it off:
builder.Services.Configure<ApiBehaviorOptions>(o =>
o.SuppressModelStateInvalidFilter = true);Middleware Pipeline
A conventional order, and why it matters
Middleware runs in the order you register it, and each piece decides whether to call the next one. Putting UseAuthorization before UseAuthentication means the authorization check runs before anyone has been identified, so every request looks anonymous. Putting UseCors after the endpoints means the CORS headers are never added. Order is not a style choice here, it is behaviour.
app.UseExceptionHandler("/error");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors("frontend");
app.UseAuthentication(); // who are you
app.UseAuthorization(); // are you allowed
app.UseOutputCache();
app.MapControllers(); // endpoints lastInline middleware
app.Use(async (context, next) =>
{
var sw = Stopwatch.StartNew();
await next(context);
Console.WriteLine($"{context.Request.Path} took {sw.ElapsedMilliseconds} ms");
});A middleware class
public class TenantMiddleware(RequestDelegate next)
{
public async Task InvokeAsync(HttpContext ctx, ITenantStore store)
{
ctx.Items["tenant"] = store.Resolve(ctx.Request.Host.Host);
await next(ctx);
}
}
app.UseMiddleware<TenantMiddleware>();Global error handling as ProblemDetails
builder.Services.AddProblemDetails();
builder.Services.AddExceptionHandler<AppExceptionHandler>();
// ...
app.UseExceptionHandler();CORS, Auth and OpenAPI
A named CORS policy
builder.Services.AddCors(o => o.AddPolicy("frontend", p =>
p.WithOrigins("https://app.example.com")
.AllowAnyHeader()
.AllowAnyMethod()));
app.UseCors("frontend");JWT bearer authentication
builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o => o.Authority = "https://login.example.com");
builder.Services.AddAuthorization(o =>
o.AddPolicy("admin", p => p.RequireRole("Admin")));
app.MapGet("/secret", () => "shh").RequireAuthorization("admin");Read the signed in user
string? id = User.FindFirstValue(ClaimTypes.NameIdentifier);
bool isAdmin = User.IsInRole("Admin");OpenAPI document, built in from .NET 9
builder.Services.AddOpenApi();
// ...
app.MapOpenApi(); // served at /openapi/v1.jsonEntity Framework Core
DbContext
Define the context and its sets
public class ShopContext(DbContextOptions<ShopContext> options)
: DbContext(options)
{
public DbSet<Order> Orders => Set<Order>();
public DbSet<Customer> Customers => Set<Customer>();
}Register it, scoped by default
builder.Services.AddDbContext<ShopContext>(o =>
o.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
// UseNpgsql, UseSqlite, UseCosmos, UseInMemoryDatabaseAn entity
public class Order
{
public Guid Id { get; set; }
public required string Reference { get; set; }
public decimal Total { get; set; }
public DateTime PlacedAt { get; set; }
public Guid CustomerId { get; set; }
public Customer Customer { get; set; } = null!;
public List<OrderLine> Lines { get; set; } = [];
}Querying
Reading single rows
Order? byKey = await db.Orders.FindAsync(id); // checks tracked entities first
Order? first = await db.Orders.FirstOrDefaultAsync(o => o.Total > 100, ct);
Order one = await db.Orders.SingleAsync(o => o.Reference == code, ct);Loading related data
Without Include, navigation properties come back empty and touching them either returns null or, with lazy loading enabled, fires a separate query per row. Loading 100 orders and then reading order.Customer.Name on each one becomes 101 round trips, which is the N plus 1 problem. Include pulls the related rows in one query, and a projection with Select is cheaper still when you only need a few columns.
var orders = await db.Orders
.Include(o => o.Customer)
.Include(o => o.Lines)
.ThenInclude(l => l.Product)
.ToListAsync(ct);Project to exactly the columns you need
var rows = await db.Orders
.Where(o => o.PlacedAt >= from)
.OrderByDescending(o => o.PlacedAt)
.Select(o => new OrderSummary(o.Id, o.Reference, o.Customer.Name, o.Total))
.Take(50)
.ToListAsync(ct);Turn off tracking for read only queries
EF tracks every entity it materialises so it can work out what changed on SaveChanges. For a read only query that work is pure overhead, and on large result sets it is a measurable amount of memory and CPU. AsNoTracking skips it. Keep tracking on only when you intend to modify and save the entities you loaded.
var report = await db.Orders
.AsNoTracking()
.Where(o => o.Total > 1000)
.ToListAsync(ct);Paging and aggregates run in SQL
var page = await db.Orders.OrderBy(o => o.Id)
.Skip((n - 1) * size).Take(size).ToListAsync(ct);
int count = await db.Orders.CountAsync(o => o.Total > 100, ct);
decimal sum = await db.Orders.SumAsync(o => o.Total, ct);
bool any = await db.Orders.AnyAsync(o => o.Reference == code, ct);Raw SQL when LINQ is not enough
var rows = await db.Orders
.FromSql($"SELECT * FROM Orders WHERE Total > {threshold}")
.ToListAsync(ct);
// interpolation here is parameterised, not concatenatedWriting
Insert, update, delete
db.Orders.Add(order);
db.Orders.AddRange(batch);
Order existing = await db.Orders.FindAsync(id);
existing.Total = 99m; // tracked, no Update call needed
db.Orders.Remove(existing);
await db.SaveChangesAsync(ct); // one transactionBulk update and delete without loading rows
await db.Orders
.Where(o => o.PlacedAt < cutoff)
.ExecuteDeleteAsync(ct); // EF 7+
await db.Orders
.Where(o => o.Status == "pending")
.ExecuteUpdateAsync(s => s.SetProperty(o => o.Status, "expired"), ct);Explicit transaction across several saves on one context
A single SaveChanges already runs in its own transaction. An explicit one is for several saves that must succeed or fail together. It covers only this context: a second DbContext opens its own connection unless you share the connection and call UseTransaction on it.
await using var tx = await db.Database.BeginTransactionAsync(ct);
try
{
db.Orders.Add(order);
await db.SaveChangesAsync(ct);
db.Payments.Add(payment);
await db.SaveChangesAsync(ct);
await tx.CommitAsync(ct);
}
catch
{
await tx.RollbackAsync(ct);
throw;
}Handle a concurrency conflict
try { await db.SaveChangesAsync(ct); }
catch (DbUpdateConcurrencyException ex)
{
foreach (var entry in ex.Entries)
await entry.ReloadAsync(ct);
}Migrations and Mapping
The commands you will run most
dotnet tool install --global dotnet-ef
dotnet ef migrations add AddOrderStatus
dotnet ef database update
dotnet ef migrations remove
dotnet ef migrations script --idempotent -o deploy.sql
dotnet ef dbcontext scaffold "..." Microsoft.EntityFrameworkCore.SqlServerFluent configuration
protected override void OnModelCreating(ModelBuilder b)
{
b.Entity<Order>(e =>
{
e.HasKey(o => o.Id);
e.Property(o => o.Reference).HasMaxLength(40).IsRequired();
e.Property(o => o.Total).HasPrecision(18, 2);
e.HasIndex(o => o.Reference).IsUnique();
e.HasMany(o => o.Lines)
.WithOne(l => l.Order)
.OnDelete(DeleteBehavior.Cascade);
});
}Data annotations, the shorter alternative
[Table("orders")]
public class Order
{
[Key] public Guid Id { get; set; }
[MaxLength(40), Required] public string Reference { get; set; } = "";
[Precision(18, 2)] public decimal Total { get; set; }
[NotMapped] public bool IsLarge => Total > 1000m;
}EF Core Pitfalls
The failures that show up in production, not in dev
| Symptom | Cause | Fix |
|---|---|---|
| Hundreds of queries per request | N plus 1 from lazy loading a navigation in a loop | Include, or project with Select |
| Whole table pulled into memory | ToList before Where, so filtering runs in C# | Filter on IQueryable, materialise last |
| Query throws at runtime | A C# method EF cannot translate to SQL | Move it after AsEnumerable, or rewrite it |
| A second operation started on this context | Two awaits on one DbContext at the same time | Await sequentially, or use a context per task |
| Cannot resolve scoped DbContext | Injected into a singleton or a hosted service | Inject IServiceScopeFactory and create a scope |
| Decimal rounding differences | No precision configured, EF logs a warning and falls back to 18,2 | Set HasPrecision explicitly |
| Migration drops a column of live data | A rename detected as delete plus add | Edit the migration to use RenameColumn |
Testing with xUnit
xUnit Basics
A single test, arrange act assert
public class BasketTests
{
[Fact]
public void Total_sums_every_line()
{
var basket = new Basket(); // arrange
basket.Add(new Line("apple", 2, 1.50m));
decimal total = basket.Total; // act
Assert.Equal(3.00m, total); // assert
}
}One test, many inputs
[Theory]
[InlineData(0, "empty")]
[InlineData(1, "single")]
[InlineData(9, "many")]
public void Describes_the_count(int count, string expected)
=> Assert.Equal(expected, Basket.Describe(count));Complex cases from a member or class source
public static TheoryData<decimal, decimal> Discounts => new()
{
{ 100m, 0m },
{ 500m, 25m },
{ 2000m, 200m }
};
[Theory]
[MemberData(nameof(Discounts))]
public void Applies_the_right_discount(decimal total, decimal expected)
=> Assert.Equal(expected, Pricing.Discount(total));Setup and teardown are the constructor and Dispose
public class DbTests : IDisposable
{
private readonly SqliteConnection _conn;
public DbTests() => _conn = OpenTestDatabase(); // before each test
public void Dispose() => _conn.Dispose(); // after each test
}Assertions
The xUnit assertions worth memorising
| Assertion | Passes when |
|---|---|
| Assert.Equal(a, b) | values are equal, expected comes first |
| Assert.Equal(a, b, 2) | doubles match to 2 decimal places |
| Assert.Same(a, b) | the same reference, not just equal |
| Assert.True / False | a boolean, pass a message for context |
| Assert.Null / NotNull | reference is or is not null |
| Assert.Contains(x, list) | the sequence contains the item |
| Assert.Empty / NotEmpty | the sequence has no or some items |
| Assert.Collection(list, ...) | each item matches its own inspector in order |
| Assert.Throws<T>(() => ...) | exactly that exception type is thrown |
| Assert.ThrowsAsync<T>(...) | the awaited call throws, must be awaited |
| Assert.IsType<T>(x) | the runtime type matches exactly |
| Assert.Matches(pattern, s) | the string matches a regular expression |
Testing that something throws
var ex = Assert.Throws<ArgumentException>(() => new Basket().Add(null!));
Assert.Equal("line", ex.ParamName);
await Assert.ThrowsAsync<HttpRequestException>(
() => client.GetAsync("/missing"));Test Doubles
Moq, the most common mocking library
var repo = new Mock<IOrderRepository>();
repo.Setup(r => r.GetAsync(It.IsAny<Guid>()))
.ReturnsAsync(new Order { Total = 50m });
var service = new OrderService(repo.Object, NullLogger<OrderService>.Instance);
await service.GetAsync(Guid.NewGuid());
repo.Verify(r => r.GetAsync(It.IsAny<Guid>()), Times.Once);NSubstitute reads a little lighter
var repo = Substitute.For<IOrderRepository>();
repo.GetAsync(Arg.Any<Guid>()).Returns(new Order());
await repo.Received(1).GetAsync(Arg.Any<Guid>());A hand written fake, often the better option
A hand written fake is often clearer than a mock. Mocks assert on interactions, which couples the test to how the code works rather than what it produces, so an internal refactor breaks tests that should not care. Reach for a mock when you genuinely need to verify a call happened, such as an email being sent, and prefer a simple in memory fake the rest of the time.
public class FakeClock(DateTimeOffset now) : IClock
{
public DateTimeOffset UtcNow { get; set; } = now;
public void Advance(TimeSpan by) => UtcNow = UtcNow.Add(by);
}Fake the clock with TimeProvider, .NET 8
var time = new FakeTimeProvider(DateTimeOffset.Parse("2026-01-01Z"));
var service = new TrialService(time);
time.Advance(TimeSpan.FromDays(31));
Assert.True(service.HasExpired());Integration Tests
Spin up the real app in memory
public class OrdersApiTests(WebApplicationFactory<Program> factory)
: IClassFixture<WebApplicationFactory<Program>>
{
[Fact]
public async Task Get_unknown_order_returns_404()
{
HttpClient client = factory.CreateClient();
var res = await client.GetAsync($"/orders/{Guid.NewGuid()}");
Assert.Equal(HttpStatusCode.NotFound, res.StatusCode);
}
}Swap a real dependency for a test one
var client = factory.WithWebHostBuilder(b =>
b.ConfigureServices(s =>
{
s.RemoveAll<IEmailSender>();
s.AddSingleton<IEmailSender, RecordingSender>();
})).CreateClient();An in memory database for EF tests
var options = new DbContextOptionsBuilder<ShopContext>()
.UseSqlite("DataSource=:memory:")
.Options;
using var db = new ShopContext(options);
await db.Database.OpenConnectionAsync();
await db.Database.EnsureCreatedAsync();Running Tests
Create the project and reference the code under test
dotnet new xunit -o Shop.Tests
dotnet add Shop.Tests reference Shop/Shop.csproj
dotnet add Shop.Tests package MoqRun, filter and collect coverage
dotnet test
dotnet test --filter "FullyQualifiedName~BasketTests"
dotnet test --filter "Category=Integration"
dotnet test --logger "console;verbosity=detailed"
dotnet test --collect:"XPlat Code Coverage"Skip a test, and group them with traits
[Fact(Skip = "flaky until the sandbox is fixed")]
public void Charges_the_card() { }
[Trait("Category", "Integration")]
public class SlowTests { }Advanced Features & dotnet CLI
Attributes and Reflection
Apply metadata to types and members
[Obsolete("Use SendAsync instead")]
public void Send() { }
[Serializable] // allowed on classes, structs, enums and delegates
public class Message { }Define your own
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class RouteAttribute : Attribute
{
public string Template { get; }
public RouteAttribute(string template) => Template = template;
}Read types and members at runtime
Type t = typeof(Order);
foreach (var p in t.GetProperties())
Console.WriteLine($"{p.PropertyType.Name} {p.Name}");
var attr = t.GetCustomAttribute<RouteAttribute>();
object? instance = Activator.CreateInstance(t);Caller info, free diagnostics
void Trace(string msg,
[CallerMemberName] string caller = "",
[CallerLineNumber] int line = 0)
=> Console.WriteLine($"{caller}:{line} {msg}");Preprocessor and Unsafe
Conditional compilation
#if DEBUG
Console.WriteLine("debug build");
#elif RELEASE
Log.Minimal();
#endif
#warning revisit before release
#pragma warning disable CS8618Pointers, only inside an unsafe context
unsafe
{
int value = 42;
int* p = &value;
Console.WriteLine(*p);
}
// requires <AllowUnsafeBlocks>true</AllowUnsafeBlocks>Stack allocation, no garbage collector pressure
Span<byte> buffer = stackalloc byte[256];dotnet CLI
Create, build, run
dotnet new console -o MyApp
dotnet new webapi -o MyApi
dotnet new classlib -o MyLib
dotnet build
dotnet run
dotnet watch runPackages, references, solutions
dotnet add package Serilog
dotnet remove package Serilog
dotnet add reference ../MyLib/MyLib.csproj
dotnet new sln -n MySolution
dotnet sln add MyApp/MyApp.csproj
dotnet restoreTest, format, publish
dotnet test
dotnet test --filter "FullyQualifiedName~Orders"
dotnet format
dotnet publish -c Release -r linux-x64 --self-contained
dotnet --list-sdksA minimal project file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>New in C# 14
Extension members, properties and static members on types you do not own
Extension methods have existed since C# 3, but only as static methods with a this parameter. C# 14 adds an extension block that can also declare extension properties, static extension members and operators for a type you do not own. The receiver is named once at the top of the block instead of on every member, and callers use the members as if they were declared on the type itself.
public static class EnumerableExtensions
{
extension<T>(IEnumerable<T> source) // instance style members
{
public bool IsEmpty => !source.Any();
public IEnumerable<T> WhereNot(Func<T, bool> pred) => source.Where(x => !pred(x));
}
extension<T>(IEnumerable<T>) // static style members
{
public static IEnumerable<T> Identity => Enumerable.Empty<T>();
}
}
if (orders.IsEmpty) { }
var none = IEnumerable<int>.Identity;Null conditional assignment, the right side runs only when the left is not null
customer?.Order = GetCurrentOrder() evaluates the right side only when customer is not null, so GetCurrentOrder() is not called at all for a null customer. Compound assignments such as += work the same way; ++ and -- are not allowed.
// before
if (customer is not null) customer.Order = GetCurrentOrder();
// C# 14
customer?.Order = GetCurrentOrder();
stats?.Hits += 1; // compound assignment works too, ++ does notnameof on an unbound generic type
Console.WriteLine(nameof(List<>)); // C# 14
Console.WriteLine(nameof(Dictionary<,>));
// before, only closed types worked: nameof(List<int>)List
DictionaryLambda parameter modifiers without spelling out the types
delegate bool TryParse<T>(string text, out T result);
TryParse<int> parse = (text, out result) => int.TryParse(text, out result);
// before: (string text, out int result) => ...Partial constructors and events, and first class spans
public partial class Widget
{
public partial Widget(string name); // defining declaration
public partial event EventHandler? Changed;
}
public partial class Widget
{
public partial Widget(string name) => Name = name; // implementing declaration
public partial event EventHandler? Changed { add { } remove { } }
}
// arrays and spans convert implicitly in more places, so span taking APIs
// accept an int[] and extension methods can have a span receiver
ReadOnlySpan<int> view = new[] { 1, 2, 3 };C# Version Highlights
What shipped when, and the .NET release that carries it
| Version | Ships with | Headline features |
|---|---|---|
| C# 8 | .NET Core 3.0 | nullable reference types, async streams, using declarations, switch expressions, default interface methods |
| C# 9 | .NET 5 | records, init only setters, top level statements, relational and logical patterns |
| C# 10 | .NET 6 | file scoped namespaces, global usings, record struct, extended property patterns |
| C# 11 | .NET 7 | raw string literals, list patterns, required members, generic math, UTF-8 literals |
| C# 12 | .NET 8 | primary constructors, collection expressions, alias any type, default lambda parameters |
| C# 13 | .NET 9 | params collections, new lock type, implicit index in initializers, ref struct in generics |
| C# 14 | .NET 10 (LTS) | extension members, field keyword, null conditional assignment, nameof on unbound generics, lambda parameter modifiers, partial constructors and events |
Compiler Errors
Type and Member Errors
The compiler cannot find something
| Code | Message | Usual cause and fix |
|---|---|---|
| CS0246 | type or namespace could not be found | A missing using, or a missing NuGet or project reference. Check the spelling, then add the using or the package. |
| CS0234 | type does not exist in the namespace | The namespace resolves but the type does not. Usually the wrong package version, or the type moved. |
| CS1061 | type does not contain a definition for X | A typo, the wrong type, or an extension method whose namespace is not imported. LINQ needs using System.Linq. |
| CS0117 | type does not contain a definition for X | You used an instance member as if it were static, or the other way round. |
| CS0103 | the name X does not exist in the current context | Out of scope. The variable was declared inside a block, a loop or a different method. |
| CS0165 | use of unassigned local variable | A path exists where the variable is never set. Give it an initial value, or assign it in every branch. |
| CS0122 | X is inaccessible due to its protection level | The member is private or internal. Make it public, or move the caller into the same assembly. |
| CS0120 | an object reference is required for the non static field | You called an instance member from a static method. Create an instance, or make the member static. |
Conversion Errors
Types do not line up
| Code | Message | Usual cause and fix |
|---|---|---|
| CS0029 | cannot implicitly convert type A to B | An assignment across incompatible types. Add an explicit cast if it is safe, or parse instead. |
| CS0266 | an explicit conversion exists, are you missing a cast | The conversion is possible but lossy. Add the cast, and think about whether the loss is acceptable. |
| CS1503 | argument N cannot convert from A to B | Arguments in the wrong order, or the wrong overload. Check the parameter list. |
| CS1502 | the best overloaded method has invalid arguments | Comes with CS1503. Read the second error, it names the offending argument. |
| CS0019 | operator cannot be applied to operands of type A and B | Comparing or adding incompatible types, often a string and a number. Parse first. |
| CS0021 | cannot apply indexing to an expression of type X | Indexing something that is not indexable, often an IEnumerable. Call ToList or ElementAt. |
| CS1929 | type does not contain a definition, the best extension overload requires a receiver of type Y | An extension method exists but for a different type, often IEnumerable of T rather than T. |
CS0029 in practice
CS0029 between string and a number is the single most common conversion error, and it almost always means a value arrived from user input, a query string, a config file or a CSV. C# will not silently coerce, which is the point. Parse the value at the boundary where it enters your code, decide what happens when it is not a number, and work with the real type from then on.
// error CS0029: cannot implicitly convert type 'string' to 'int'
int age = Console.ReadLine();
// fix
int age = int.Parse(Console.ReadLine()!);
// better, it never throws
if (!int.TryParse(Console.ReadLine(), out int age)) age = 0;Nullability Warnings
These appear the moment you enable nullable reference types
| Code | Message | Usual cause and fix |
|---|---|---|
| CS8618 | non nullable property must contain a non null value when exiting the constructor | Mark it required, give it a default, set it in the constructor, or make the type nullable. |
| CS8602 | dereference of a possibly null reference | Guard with a null check, use ?. or ??, or narrow with is not null first. |
| CS8600 | converting null literal or a possible null value to a non nullable type | The right hand side may be null. Declare the variable as nullable. |
| CS8603 | possible null reference return | The method promises non null but a path returns null. Change the return type to T?. |
| CS8604 | possible null reference argument | You are passing a maybe null value into a parameter that does not accept null. |
| CS8625 | cannot convert null literal to a non nullable reference type | You passed null explicitly. Make the parameter nullable, or pass a real value. |
The four ways to silence CS8618, in order of preference
public required string Name { get; init; } // caller must supply it
public string Name { get; set; } = ""; // sensible default
public Order(string name) => Name = name; // set in the constructor
public string Name { get; set; } = null!; // last resort, a promise onlyAsync Errors
await and Task problems
| Code | Message | Usual cause and fix |
|---|---|---|
| CS4032 | await can only be used within an async method | Add async to the enclosing method and change its return type to Task. |
| CS4033 | await can only be used within an async method | Same, but in a void returning method. Return Task instead of void. |
| CS1998 | this async method lacks await operators and will run synchronously | Either await something, or drop async and return Task.FromResult or Task.CompletedTask. |
| CS4014 | because this call is not awaited, execution continues before the call is completed | You forgot an await. If it is deliberate fire and forget, assign to a discard and handle the exceptions. |
| CS1061 | Task does not contain a definition for the result property you want | You forgot to await, so you are looking at the Task rather than its value. |
| CS8419 | the body of an async iterator must contain a yield | An async IAsyncEnumerable method with no yield return. |
CS4014, and what to do when it is intentional
// warning CS4014
SendEmailAsync(user);
// fix, await it
await SendEmailAsync(user);
// deliberate fire and forget, never let it throw unobserved
_ = SendEmailAsync(user).ContinueWith(
t => logger.LogError(t.Exception, "email failed"),
TaskContinuationOptions.OnlyOnFaulted);Runtime Exceptions You Will Meet
These compile fine and fail in production
| Exception | Usual cause and fix |
|---|---|
| NullReferenceException | Something in the chain was null. Turn on nullable reference types, then use ?. and null checks rather than hunting at runtime. |
| InvalidOperationException: collection was modified | You added or removed inside a foreach over the same collection. Iterate a copy with ToList, or build a second list. |
| InvalidOperationException: sequence contains no elements | First, Single, Max or Average on an empty sequence. Use FirstOrDefault, or check Any first. |
| InvalidOperationException: sequence contains more than one element | Single matched twice. Use First if duplicates are acceptable, otherwise fix the data or the filter. |
| KeyNotFoundException | Reading a dictionary key that is not there. Use TryGetValue or GetValueOrDefault. |
| ObjectDisposedException | Using a stream, connection or HttpClient after its using block ended, often because an async call outlived the scope. |
| IndexOutOfRangeException | An off by one on an array. Remember the last index is Length minus 1, or use the ^1 index. |
| StackOverflowException | Unbounded recursion, or a property whose getter returns itself instead of the backing field. |
| FormatException | Parse on text that is not a number or date, often a culture difference in the decimal separator. Use TryParse with an explicit culture. |
| TaskCanceledException | A token fired, or an HttpClient timeout elapsed. Check Timeout before blaming the token. |
Java & Python to C#
Java to C#
Syntax that looks familiar but is not identical
| Java | C# | Note |
|---|---|---|
| package com.app; | namespace App; | No folder to namespace requirement |
| import java.util.List; | using System.Collections.Generic; | Imports a namespace, not a type |
| final int x = 1; | const int x = 1; | readonly for run time, const for compile time |
| final class Foo | sealed class Foo | Cannot be inherited |
| @Override | override | A keyword, and it is mandatory |
| getName() / setName() | Name { get; set; } | Properties replace accessor pairs |
| String | string | Lowercase alias for System.String |
| ArrayList<T> | List<T> | Same idea, different name |
| HashMap<K,V> | Dictionary<K,V> | TryGetValue instead of get returning null |
| stream().filter().map() | .Where().Select() | LINQ, and it is lazy too |
| Optional<T> | T? plus nullable context | No wrapper object |
| throws IOException | nothing | C# has no checked exceptions |
| interface with default | interface with default | Same feature since C# 8 |
| record Point(int x) | record Point(int X) | Nearly identical, C# adds with |
| var (Java 10) | var | Same, but C# has had it since 3.0 |
| System.out.println | Console.WriteLine | |
| instanceof Foo f | is Foo f | Pattern matching, C# goes much further |
| synchronized | lock | Same monitor idea |
| CompletableFuture<T> | Task<T> | With async and await on top |
The three habits to unlearn
The habit that trips Java developers hardest is writing getters and setters by hand. In C# a property is the idiomatic public surface, and turning a field into a property later is a binary breaking change, so start with properties. The second habit is checked exceptions: C# has none, so nothing forces you to handle an error, and unhandled exceptions surface only at runtime.
// 1. properties, not accessor pairs
public string Name { get; set; }
// 2. no checked exceptions, so document what you throw
/// <exception cref="IOException">the file is locked</exception>
// 3. value types exist, a struct is copied on assignment
struct Point { public int X, Y; }Python to C#
The everyday equivalents
| Python | C# | Note |
|---|---|---|
| print(x) | Console.WriteLine(x); | Semicolons and braces, not indentation |
| f"hi {name}" | $"hi {name}" | Same idea, dollar instead of f |
| list | List<T> | Homogeneous, the element type is fixed |
| dict | Dictionary<K,V> | TryGetValue instead of dict.get |
| set | HashSet<T> | |
| tuple | (int, string) or record | Named elements are supported |
| [x*2 for x in xs if x>0] | xs.Where(x => x > 0).Select(x => x * 2) | LINQ replaces comprehensions |
| len(xs) | xs.Count or xs.Length | Count for collections, Length for arrays |
| xs[-1] | xs[^1] | Index from end |
| xs[1:4] | xs[1..4] | Range, end exclusive in both |
| def __init__(self) | public Foo() | Constructor, no explicit self |
| self | this | Implicit, not a parameter |
| @decorator | [Attribute] | Metadata, not a wrapper function |
| with open(p) as f: | using var f = File.OpenRead(p); | Deterministic disposal |
| yield | yield return | Same lazy generator idea |
| None | null | Only reference types and T? can hold it |
| try / except / finally | try / catch / finally | catch, not except |
| async def / await | async Task / await | Very close, no event loop to start |
| pip install x | dotnet add package x | |
| if __name__ == "__main__" | top level statements | The file that has them is the entry point |
The mental shift
The real adjustment coming from Python is not syntax, it is that the compiler runs before the program does. Types are checked, names are resolved and unreachable paths are found at build time, so a whole class of mistakes never reaches a test. In exchange you declare more up front. Lean on var to keep it readable, and let the IDE tell you the inferred type.
// Python: shape is discovered at run time
// C#: shape is declared, then checked at build time
var names = new List<string>(); // var still infers
names.Add("Ana");
names.Add(42); // error CS1503 at build timeFirst Week Gotchas
Things that surprise almost every newcomer
7 / 2 // 3, not 3.5, both operands are int
"a" == "a" // true, string overloads ==
obj1 == obj2 // reference equality unless overloaded
struct copies // assignment copies the whole value
foreach var // the loop variable is per iteration since C# 5
switch fallthrough // not allowed: a non empty case ends in break, return or goto
array.Length // arrays use Length, lists use CountNaming conventions differ from both languages
PascalCase types, methods, properties, constants, enum members
camelCase locals and parameters
_camelCase private fields
IPascalCase interfaces
TPascalCase generic type parameters
NameAsync asynchronous methodsNothing matches. Try a single word.
What Is a C# Cheat Sheet
A C# cheat sheet is a single page reference to the language's syntax, types, operators and the .NET libraries every program touches, organised so that a working developer finds the right line in seconds instead of re-reading a chapter of the documentation. The search variants csharp cheat sheet and C sharp cheat sheet mean the same thing.
This C# cheat sheet covers 24 sections and 358 snippets, from Hello World to ASP.NET Core, Entity Framework Core and xUnit, and every card that depends on a newer language version names it under its title. Each section is a set of cards, each card a set of copyable examples with a one line description, an expected output where one exists, and a short explanation of why the feature behaves the way it does.
It is written for developers who know how to program and want the C# way of doing something without a detour through Microsoft Learn. Read it top to bottom as a map of the language and the platform, or use the search box to jump to the line you need.
How It Differs From the Documentation
The documentation explains; this page reminds. Every entry is the shortest working form of a feature, placed next to the mistake people usually make with it, with the whole language on one scrollable page behind one search box. When a snippet is not enough, the relevant Microsoft Learn chapter is one link away.
What This C# Cheat Sheet Covers
The 24 sections run from the language core outward to the platform, and each link below jumps to that section of the reference above.
- Fundamentals: program structure and console I/O, C# types and variables, operators, strings and formatting, control flow and pattern matching.
- Types and objects: methods and parameters, classes, properties and constructors, records and structs, inheritance and interfaces, and generics.
- Data: collections and arrays and LINQ queries.
- Behaviour: delegates, lambdas and events, async and concurrency, exceptions, files and JSON, and everyday APIs such as dates, Guid, Random and HttpClient.
- Platform: dependency injection, configuration and logging, ASP.NET Core, Entity Framework Core, and testing with xUnit.
- Tooling and help: attributes, reflection, the dotnet CLI and what is new in C# 14, compiler errors and runtime exceptions, and translation tables for developers coming from Java or Python.
What C# Is
C# is a statically typed, object oriented language that compiles to intermediate language and runs on the .NET runtime. Anders Hejlsberg designed it at Microsoft, and version 1.0 shipped in 2002.
Static typing means the compiler knows the type of every expression before the program runs. Most mistakes surface at build time rather than in production.
The name is written C#, and spelled csharp in URLs, file paths, package names and tags, because the sharp sign has no place in a web address. Both refer to the same language, so a search for either lands you here.
The language has picked up functional features along the way: lambdas, LINQ, immutable records and pattern matching. It is no longer purely object oriented, and modern C# code often reads closer to F# than to Java.
Everything on this page runs on .NET, which is free, open source and cross platform. Console apps, web apps and libraries run unchanged on Windows, Linux and macOS; WPF and WinForms are Windows only.
Where C# Runs
- Web APIs and sites: ASP.NET Core, Minimal APIs, Blazor for browser side code compiled to WebAssembly.
- Desktop: WPF, WinForms, WinUI 3 and Avalonia for cross platform desktop.
- Mobile and multi platform: .NET MAUI targets Android, iOS, macOS and Windows from one project.
- Games: Unity uses C# as its scripting language, which makes it one of the most widely used game development languages in the world.
- Cloud and services: Azure Functions, AWS Lambda, worker services, gRPC and background processing.
- Tooling: command line utilities, source generators, Roslyn analyzers and build tasks.
C# Syntax Basics
A C# file is a sequence of type declarations inside namespaces. Since C# 9 a simple program can skip all of that.
The Two Program Shapes
Top level statements let a console app be one line. The compiler generates the class and the Main method for you.
Console.WriteLine("Hello");
The explicit form is still what you get in libraries and larger apps:
namespace MyApp;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello");
}
}
Both compile to the same thing. Pick top level statements for scripts, samples and small tools, and the explicit form when a file holds more than one type.
Statements, Blocks and Semicolons
Every statement ends with a semicolon. Blocks use braces. Whitespace is not significant, so indentation is style rather than syntax.
C# is case sensitive. Total and total are two different identifiers, and the convention is PascalCase for types and public members, camelCase for locals and parameters, and a leading underscore for private fields.
Value Types and Reference Types
This is the distinction that explains most surprising behaviour in C#.
Value types (int, double, bool, char, struct, enum, record struct) hold their data directly. Assigning one copies the data. They usually live on the stack or inline inside another object.
Reference types (class, record, interface, delegate, string, array) hold a reference to data on the heap. Assigning one copies the reference, so both variables now point at the same object.
var a = new List<int> { 1 };
var b = a;
b.Add(2);
Console.WriteLine(a.Count); // 2, same list
int x = 1;
int y = x;
y++;
Console.WriteLine(x); // 1, separate copies
Strings are the exception that trips people up: they are reference types, but they are immutable, so they behave like values. Every operation that looks like a mutation returns a new string.
Nullability in C#
Null reference exceptions were the single most common runtime failure in C# for two decades. C# 8 introduced nullable reference types to move that problem to compile time.
With <Nullable>enable</Nullable> in the project file, string means "never null" and string? means "may be null". The compiler warns when you dereference something it cannot prove is non null.
This is a compile time analysis only. Nothing is checked at runtime, and a value that arrives from JSON, a database or an older library can still be null despite the annotation. Treat the warnings as a strong hint, not a guarantee.
The Null Operators
| Operator | Meaning | Example |
|---|---|---|
| ?. | member access, short circuits to null | user?.Name |
| ?[] | indexer access, short circuits to null | items?[0] |
| ?? | fallback when the left side is null | name ?? "guest" |
| ??= | assign only when currently null | cache ??= Load() |
| ! | suppress the warning, no runtime check | value!.Length |
| is null | null test that ignores operator overloads | if (x is null) |
Prefer is null and is not null over == null. A type can overload ==, and the pattern form always does the plain reference check.
Collections and LINQ
Almost every C# program manipulates sequences, and LINQ is the query language built into the framework for doing it.
Choosing a Collection
List<T> is the default. Reach for Dictionary<TKey, TValue> the moment you find yourself scanning a list to find something by a key, and for HashSet<T> when you only care whether an item is present.
The cost difference is real. Looking up an item in a list of 10,000 entries scans on average 5,000 of them. The same lookup in a dictionary hashes the key once.
Deferred Execution
LINQ operators do not run when you write them. They build a query that runs when you enumerate it.
var query = people.Where(p => p.IsActive); // nothing happened yet
foreach (var p in query) { } // runs here
var list = query.ToList(); // runs again
Two consequences follow. First, enumerating twice does the work twice, so materialise with ToList() when you need the results more than once. Second, if the source changes between building the query and running it, the results reflect the new state.
IEnumerable versus IQueryable
IEnumerable<T> runs in memory using delegates. IQueryable<T> builds an expression tree that a provider such as Entity Framework Core translates into SQL.
The practical rule: call ToList() as late as possible when querying a database, so filtering happens in SQL rather than after pulling every row into memory.
Async and Await Explained
An async method returns a Task that represents work in progress. await suspends the method, returns control to the caller, and resumes when the task completes.
Nothing here creates a thread. For an HTTP call or a database query, the thread is returned to the pool while the network does its work, which is why a small pool can serve thousands of concurrent requests.
The Rules Worth Memorising
- Async all the way down. Calling
.Resultor.Wait()on a task blocks the thread and can deadlock in contexts that capture a synchronisation context. - Return Task, not void. An
async voidmethod cannot be awaited and its exceptions crash the process. Event handlers are the only exception. - Start before you await. To run two calls concurrently, start both tasks and then await them, or use
Task.WhenAll. - Pass the CancellationToken. A token that stops at the first layer is a token that does nothing.
- Do not await inside a lock. Use
SemaphoreSlimwhen the critical section contains asynchronous work.
Awaiting sequentially in a loop is a common cause of slow endpoints. Two calls that each take 200 ms take 400 ms in sequence and roughly 200 ms with Task.WhenAll.
Records, Classes and Structs
C# now offers four ways to declare a type, and the choice is about semantics rather than speed.
Use a class for anything with identity and behaviour: services, entities, controllers, anything you would describe as "the" thing rather than "a" value.
Use a record for data that is defined by its contents: DTOs, API contracts, messages, query results, configuration. You get value equality, a readable ToString, deconstruction and the with expression at no cost.
Use a struct for small immutable values that are created in large numbers, such as coordinates, identifiers and money. The rough guideline is 16 bytes or less, because larger structs are expensive to copy.
Use a record struct when you want both value semantics and value equality.
The with Expression
var original = new Person("Ana", "Pop");
var updated = original with { LastName = "Ionescu" };
This creates a copy with one property changed. The original is untouched, which is what makes records safe to share across threads and cache without defensive copying.
Pattern Matching
Pattern matching started as a nicer if (x is Type t) and has grown into a small sublanguage. It is now the clearest way to express branching logic over shapes of data.
string Describe(object o) => o switch
{
null => "nothing",
int n when n < 0 => "negative number",
int n => $"number {n}",
string { Length: 0 } => "empty string",
string s => $"text of {s.Length}",
int[] { Length: > 0 } arr => $"array starting at {arr[0]}",
_ => "something else"
};
The compiler checks exhaustiveness on switch expressions. If no arm matches at runtime you get a SwitchExpressionException, so a discard arm is usually worth adding.
Common C# Mistakes
These ten mistakes account for most of the C# bugs that reach production, and every one of them is easy to avoid once you know it exists.
- Concatenating strings in a loop. Each
+=allocates a new string. UseStringBuilderpast a handful of iterations. - Comparing floating point numbers with ==. Use a tolerance, or use
decimalfor money. - Catching Exception and swallowing it. An empty catch block hides bugs. Catch the specific type, or let it propagate.
- Using throw ex instead of throw. The first resets the stack trace and destroys the evidence.
- Creating a new HttpClient per request. Sockets leak. Use
IHttpClientFactoryor a single shared static instance. - Modifying a collection while iterating it. Build a second list, or iterate a snapshot with
ToList(). - Calling Count() on an IEnumerable to test emptiness.
Any()stops at the first element. - Forgetting to dispose. Streams, connections and HTTP responses need
using. - Overriding Equals without GetHashCode. Dictionary and HashSet lookups then fail silently.
- Public mutable fields. Use properties, so you can add validation later without a breaking change.
What Changed in C# 12, 13 and 14
C# ships a new major version every November alongside .NET, and the last three releases each added a feature you will see in new code immediately.
- C# 12 with .NET 8 (November 2023): primary constructors on any class or struct, collection expressions with the spread element, alias any type, default values on lambda parameters.
- C# 13 with .NET 9 (November 2024):
paramson any collection type includingSpan<T>, the newSystem.Threading.Locktype, implicit index access in object initializers,ref structtypes in generics and interfaces, partial properties. - C# 14 with .NET 10 (November 2025): extension members that add properties and static members to types you do not own, the
fieldkeyword for property backing fields, null conditional assignment with?.on the left side,nameofon unbound generic types, lambda parameter modifiers without types, partial constructors and events, and first class span conversions.
.NET 10 is a long term support release, so C# 14 is the version a new project should target. Everything from C# 8 onward runs unchanged on it.
C# Naming Conventions
C# naming follows one rule almost everywhere: PascalCase for anything public or type level, camelCase for locals and parameters, and a leading underscore for private fields.
| Element | Convention | Example |
|---|---|---|
| Class, record, struct | PascalCase | OrderService |
| Interface | PascalCase with an I prefix | IOrderRepository |
| Method, property, event | PascalCase | CalculateTotal |
| Local variable, parameter | camelCase | orderTotal |
| Private field | underscore then camelCase | _repository |
| Constant | PascalCase | MaxRetries |
| Type parameter | T, or T then a name | TKey, TResult |
| Async method | PascalCase with an Async suffix | GetOrderAsync |
| Enum type and members | PascalCase, singular unless flags | OrderStatus.Paid |
These come from the Microsoft coding conventions, and every mainstream analyzer enforces them. Add an .editorconfig to your solution and the IDE will apply them for you.
How to Practise C#
The fastest way to learn C# is to write small programs against real data with the compiler set to its strictest settings, and this cheat sheet open beside the editor. Reading a reference page is not the same as writing code, so a few things that work:
- Install the .NET SDK and run
dotnet new console. The whole loop from empty folder to running program takes under a minute. - Use
dotnet watch runso the app restarts on every save. - Write small programs against real data. Parse a CSV, call a public API, count words in a text file.
- Turn on
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>early. The compiler is a better teacher than a tutorial. - Read the source. The .NET runtime and libraries are on GitHub, and the standard library is written in idiomatic C#.
When something on this page is unclear, the official C# documentation is thorough and kept current with each release. For the other half of the job, our SQL cheat sheet covers the queries Entity Framework generates, the regex cheat sheet covers the patterns you will pass to System.Text.RegularExpressions, the Git cheat sheet covers the workflow around every commit, and if you are arriving from another language the Java and Python cheat sheets pair with the translation tables in the last section.
How This C# Cheat Sheet Is Maintained
This page is written and maintained by Bogdan Sandu for TMS Outsource, a software development agency that ships .NET work for clients. The current revision was checked against the C# 14 and .NET 10 documentation on October 2, 2026, and every example that shows an output was compiled and run on .NET 10, and the versions noted on the cards reflect that check. The plan is to revise it with each November release of C#; the date in the byline at the top of this article shows the last revision. Corrections are welcome through the contact details in the footer.
C# Questions People Actually Ask
The questions developers search for most about C# and about this cheat sheet, answered in a few sentences each.
What is C# used for?
C# is a statically typed, object oriented language that runs on .NET. It is used for web APIs and sites with ASP.NET Core, desktop apps with WPF and WinUI, cross platform apps with .NET MAUI, games with Unity, cloud functions, background services and command line tools. The same code runs on Windows, Linux and macOS.
What is the difference between a class, a struct and a record in C#?
A class is a reference type with reference equality. A struct is a value type that is copied on assignment and compared field by field. A record is a reference type by default that adds value based equality, a generated ToString, deconstruction and non destructive copying with the with expression. Use record struct when you want value semantics and value equality together.
When should I use async and await in C#?
Use async and await for input and output bound work such as HTTP calls, database queries and file access, so the calling thread is released while the operation is pending. For CPU bound work use Task.Run or the Parallel class instead. Return Task or Task<T> rather than void, except in event handlers, and never block on a task with .Result or .Wait().
What does the question mark mean in C# types?
On a value type, int? means Nullable<int>, a value that can also hold null. On a reference type, string? is a nullable reference annotation that tells the compiler null is expected there. The ?. operator short circuits member access when the left side is null, ?? supplies a fallback value, and ??= assigns only when the target is currently null.
What is LINQ in C#?
LINQ is a set of query operators built into .NET for filtering, projecting, ordering, grouping and aggregating sequences. It comes in method syntax, such as items.Where(x => x.IsActive).Select(x => x.Name), and query syntax that reads like SQL. LINQ queries are lazy, so nothing runs until you enumerate the result or call ToList, ToArray, Count or First.
Which C# version should I target?
The language version follows the target framework. Targeting net8.0 gives you C# 12, net9.0 gives you C# 13, and net10.0 gives you C# 14 with extension members and null conditional assignment. .NET 10 is the current long term support release, so new projects should target it, and set Nullable and ImplicitUsings to enable in the project file so you start with modern defaults.
Is this C# cheat sheet up to date?
Yes. It was revised on October 2, 2026 against the C# 14 and .NET 10 documentation. It covers C# 8 through C# 14, every card that depends on a newer language version names it under its title, and the date in the article byline changes with every revision.
Can I download this C# cheat sheet as a PDF?
Yes. The page has a print stylesheet, so your browser's print command with Save as PDF produces a clean copy: every expected output and explanation is included, and the search box, navigation and footer are left out. Run a search first to print only the snippets that are still visible.