Advertisement
Developer Tools & Cloud Infrastructure Sponsor Zone

C# 13 & LINQ Performance Cheatsheet

Master C# Language Integrated Query (LINQ), deferred execution, async streams, records, and memory-efficient spans.

Advertisement
Developer Cloud IDE & Database Sponsor

1. Essential LINQ Transformations

LINQ operators use deferred execution until materialized by ToList(), ToArray(), or enumeration via foreach.

CODE SNIPPET
// Filtering and transforming collections
var highValueCustomers = customers
    .Where(c => c.IsActive && c.TotalPurchases > 1000)
    .OrderByDescending(c => c.TotalPurchases)
    .Select(c => new { c.Id, c.FullName, Tier = "VIP" })
    .ToList();

// GroupBy aggregation
var salesByCategory = orders
    .GroupBy(o => o.Category)
    .Select(g => new { Category = g.Key, TotalRevenue = g.Sum(x => x.Price) });

2. C# Records and Value Equality

Records provide built-in value-based equality, concise syntax, and safe immutability for domain models and DTOs.

CODE SNIPPET
// Positional record declaration with immutable properties
public record Developer(string Name, string Language, int ExperienceYears);

// Non-destructive mutation using 'with' expression
var dev1 = new Developer("Alex", "C#", 5);
var dev2 = dev1 with { Language = "F#" };

// Value-based equality check (returns true)
bool areEqual = dev1 == new Developer("Alex", "C#", 5);

3. Memory Optimization with ReadOnlySpan<T>

ReadOnlySpan allows slicing contiguous memory without triggering heap allocations or garbage collection pressure.

CODE SNIPPET
// Zero-allocation string parsing using ReadOnlySpan
string log = "2026-09-12|ERROR|ServiceUnavailable";
ReadOnlySpan<char> span = log.AsSpan();

int firstDelimiter = span.IndexOf('|');
ReadOnlySpan<char> dateSpan = span.Slice(0, firstDelimiter);
ReadOnlySpan<char> rest = span.Slice(firstDelimiter + 1);

int secondDelimiter = rest.IndexOf('|');
ReadOnlySpan<char> levelSpan = rest.Slice(0, secondDelimiter);

Need another cheat sheet?

We add new reference guides every week based on community requests.

Request a Cheatsheet →