C# Interview Question #99

What are common C# performance best practices?

SOLID, DI, Testing & Practical C# Mid-Level Intermediate

Quick Interview Answer

Performance optimization should be driven by measurement rather than assumptions. Profiling, tracing, metrics, database execution plans, and benchmarks help identify actual bottlenecks.

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.