What are common C# performance best practices?
Quick Interview Answer
Detailed Explanation
Performance optimization should be driven by measurement rather than assumptions. Profiling, tracing, metrics, database execution plans, and benchmarks help identify actual bottlenecks.
Common practices include using asynchronous I/O for database and HTTP operations, avoiding unnecessary allocations, selecting appropriate collections, using StringBuilder for repeated large string construction when appropriate, avoiding unnecessary boxing, and reducing repeated enumeration.
In EF Core applications, major improvements often come from selecting only required columns, using AsNoTracking for read-only queries, avoiding N+1 queries, using pagination, creating appropriate database indexes, and keeping filtering on IQueryable so it can execute in SQL.
Caching can improve performance for stable and frequently requested data, but invalidation and consistency must be designed carefully.