How can you optimize the memory usage of a collection in C#?
Short answer: Pre-allocate capacity when you know the expected size (e.g., new List<T>(capacity)) to avoid frequent resizing.
Explain a bit more
Use value types or structs when appropriate to reduce reference overhead. Choose collections with lower overhead for your use case (e.g., List<T> instead of LinkedList<T> if indexing is needed). Use immutable collections or pooling to minimize allocations. Avoid unnecessary boxing/unboxing by using generic collections instead of non-generic. Regularly trim collections if supported (e.g., List<T>.TrimExcess()). 📘 C# Advanced Collection Topics – Interview Questions & Answers
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png