What is a Dynamic Array?
💡 Concept Name
Dynamic Array – A flexible collection that expands and shrinks to fit your data, unlike a basic fixed-size array. In .NET, the go-to version is List<T>
.
📘 Quick Intro
Dynamic arrays take the pain out of guessing how much data you'll need to store. Just keep adding elements, and the collection grows for you behind the scenes—no manual resizing required.
🧠 Analogy / Short Story
Imagine hosting a surprise party and not knowing how many friends will show up. You start with a few chairs, but as more guests arrive, you quickly grab more from the next room—no one’s left standing. That’s how dynamic arrays handle your growing data!
🔧 Technical Explanation
- 📦 Starts with an underlying array that doubles in size whenever more space is needed.
- ➕ When you add an item and there's no room left, it creates a bigger space and copies everything over (the "resize" moment).
- ⚡ You still get fast, constant-time O(1) access by index, just like with normal arrays.
- 🔁 Resizing is rare, but when it happens, it's O(n) since every item is moved once.
- 🔧 In C#/.NET,
System.Collections.Generic.List<T>
is the standard dynamic array class.
🎯 Purpose & Use Case
- ✅ Great when you don't know in advance how much data you'll store (think user input, real-time feeds, or building a list on the fly).
- ✅ Ideal for apps where you’re adding or removing data often—dynamic arrays save you from outgrowing your storage.
- ✅ Widely used in modern business logic, from cart items in shopping apps to dynamic search suggestions.
💻 Real Code Example
// Using List in C#
var superheroList = new List<string>();
superheroList.Add("Iron Man");
superheroList.Add("Wonder Woman");
superheroList.Add("Black Panther");
foreach (var hero in superheroList)
{
Console.WriteLine(hero); // Prints each superhero's name
}
// Add as many heroes as you like—the list grows as needed!

❓ Interview Q&A
Q1: What is a dynamic array?
A: It's a flexible array that automatically resizes as you add or remove items.
Q2: What is the standard dynamic array in .NET?
A: List<T>
from System.Collections.Generic
.
Q3: How does resizing actually work?
A: The list creates a new, larger internal array and copies existing items to the new space.
Q4: Is accessing an element by index still fast?
A: Yes—constant time, just like arrays.
Q5: Do resizes slow things down?
A: Occasionally, but only while resizing. Most adds are instant.
Q6: Can you control the size or memory of a List<T>?
A: Yes, use the Capacity
property or call TrimExcess()
to adjust.
Q7: Can a dynamic array mix types?
A: No, they're type-safe—stick to the type you declare.
Q8: Are dynamic arrays thread-safe?
A: Not by default. Use locking or concurrent collections if needed.
Q9: When might you avoid dynamic arrays?
A: If you always know the size up front, or need ultra-tight memory usage, plain arrays might be better.
Q10: Can you use a foreach
loop with List<T>?
A: Absolutely—it’s built for easy iteration.
📝 MCQs
Q1. What's a dynamic array?
- A linked list
- A fixed-size array
- A flexible, resizable collection
- A stack
Q2. Which .NET type expands as you add items?
- ArrayList
- List<T>
- Hashtable
- Queue
Q3. What happens when List<T> needs more space?
- It throws an error
- It resizes and copies all elements
- It deletes items
- Nothing changes
Q4. Is access by index fast in List<T>?
- No (O(n))
- Yes (O(1))
- O(log n)
- It depends
Q5. Can you use foreach with a dynamic array?
- No
- Yes
- Only with integers
- Only in C++
Q6. Is List<T> thread-safe?
- Yes
- No
- Only with reference types
- Only after .NET 6
Q7. What does TrimExcess() do?
- Sorts the list
- Deletes empty items
- Reduces memory to fit actual items
- Doubles the list size
Q8. How are dynamic arrays different from regular arrays?
- They grow as needed
- They are value types
- They can't store strings
- They use less memory
Q9. When should you NOT use List<T>?
- When memory usage is critical
- When you need dynamic storage
- When building a stack
- When sorting data
Q10. Are dynamic arrays type-safe?
- Yes
- No
- Only for ints
- Only for objects
💡 Bonus Insight
Dynamic arrays are everywhere—from search results that grow as you scroll, to shopping carts that fit any number of items. If your app is unpredictable or needs to react to the user, dynamic arrays keep your code clean and stress-free.
📄 PDF Download
Need a handy summary for your notes? Download this topic as a PDF!