IEnumerable vs ICollection vs IList?
IEnumerable vs ICollection vs IList
What interviewers test
- Execution pipeline understanding
- Abstraction knowledge
Hierarchy
IEnumerable
└── ICollection
└── IList
IEnumerable<T>
- Read-only iteration
- Lazy execution
IEnumerable<int> numbers = GetNumbers();
foreach (var n in numbers) { }
✅ Best for streaming, read-only, deferred execution
ICollection<T>
- Adds count + add/remove
ICollection<int> list = new List<int>();
list.Add(1);
✅ When modifying collection without index access
IList<T>
- Index-based access
IList<int> list = new List<int>();
int first = list[0];
✅ When order & index matter
Interview rule of thumb
“Expose the least powerful interface required.”