Performance Optimization & Profiling
Prerequisites: Testing strategies
Measure before optimizing — Visual Studio's built-in profiler (or
dotnet-trace) shows where a running .NET program actually spends
its time and memory.
Common, high-value fixes once a real bottleneck is found:
- Choosing the right collection — a
Dictionarylookup is O(1), a linearListsearch is O(n), exactly as in Java. - Avoiding unnecessary allocations in a hot loop (e.g.
stringconcatenation with+instead ofStringBuilder, revisited from Level 1 now at scale). - Batching database calls through EF Core instead of triggering a query per item in a loop (the same N+1 query problem covered for Java).
Common mistakes
- "Optimizing" code with no profiler data to justify it — as in every language covered so far, this usually costs readability for no measurable benefit.