Index · Common pitfalls

1 min read
Senior6 min read
Rapid overview

Common pitfalls

Bad PracticeWhy it’s badFix
Using new objects inside tight loopsFloods Gen 0Reuse pooled objects
string.Concat or + in loopsCreates new string every timeUse StringBuilder or spans
LINQ in hot pathsAllocates enumerators, closuresUse for loops
Boxing value typesAllocates on heapUse generics / avoid casting to object
Repeatedly allocating buffersLOH churnUse ArrayPool<T>
Returning large arraysLOH growthReuse pooled arrays or slice spans

See also