Mid Collections

How can you optimize the memory usage of a collection in C#?

Follow:

  • Pre-allocate capacity when you know the expected size (e.g., new

List<T>(capacity)) to avoid frequent resizing.

  • 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

More from C# Programming Tutorial

All questions for this course