What is the difference between IEnumerable and IEnumerator?
๐ก Concept: IEnumerable vs IEnumerator
IEnumerable and IEnumerator are interfaces used to iterate over collections in C# but serve different roles.
๐ Quick Intro
IEnumerable provides an enumerator to iterate a collection, while IEnumerator performs the actual iteration.
๐ง Analogy
Think of IEnumerable as a book and IEnumerator as the bookmark that moves through the pages.
๐ง Technical Explanation
- ๐ IEnumerable declares GetEnumerator() method returning IEnumerator.
- ๐ IEnumerator provides Current property and MoveNext()/Reset() methods.
- โ๏ธ IEnumerable enables use of foreach loops.
- ๐ ๏ธ IEnumerator controls the position during iteration.
- ๐ก IEnumerable is typically implemented by collections; IEnumerator by enumerators.
๐ฏ Use Cases
- โ Use IEnumerable to expose an iterator for collections.
- โ Use IEnumerator to traverse elements one by one.
- โ Custom collections implement IEnumerable for iteration support.
- โ IEnumerator manages internal state of iteration.
๐ป Code Example
using System;
using System.Collections;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int>() {1, 2, 3};
// Using IEnumerable
IEnumerable<int> enumerable = numbers;
IEnumerator<int> enumerator = enumerable.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
}
}

โ Interview Q&A
Q1: What does IEnumerable provide?
A: Method to get an enumerator.
Q2: What does IEnumerator do?
A: Iterates over collection elements.
Q3: Can IEnumerable be used in foreach?
A: Yes.
Q4: What methods does IEnumerator have?
A: MoveNext, Reset, Current.
Q5: Who implements IEnumerable?
A: Collections like List, Array.
Q6: Is IEnumerator reusable?
A: Usually not; a new one is created for each iteration.
Q7: What is the return type of GetEnumerator?
A: IEnumerator.
Q8: Can IEnumerator modify collection?
A: No.
Q9: Difference between IEnumerator and IEnumerator
A: IEnumerator
Q10: What namespace contains IEnumerable?
A: System.Collections or System.Collections.Generic.
๐ MCQs
Q1. What does IEnumerable provide?
- Method to get enumerator
- Iterator
- Enumerator
- Indexer
Q2. What does IEnumerator do?
- Creates collection
- Deletes elements
- Iterates elements
- Stores elements
Q3. Can IEnumerable be used in foreach?
- No
- Yes
- Sometimes
- Never
Q4. IEnumerator methods?
- Move, Reset, Current
- MoveNext, Reset, Current
- Add, Remove, Update
- Start, Stop, Current
Q5. Who implements IEnumerable?
- Collections
- Enumerators
- Interfaces
- Classes
Q6. Is IEnumerator reusable?
- Yes
- No
- Sometimes
- Always
Q7. GetEnumerator return type?
- IEnumerable
- IEnumerator
- Collection
- Object
Q8. Can IEnumerator modify collection?
- Yes
- No
- Sometimes
- Always
Q9. IEnumerator vs IEnumerator<T>?
- Non-generic
- Generic version
- Deprecated
- Internal
Q10. Namespace for IEnumerable?
- System.IO
- System.Collections or System.Collections.Generic
- System.Text
- System.Threading
๐ก Bonus Insight
Understanding IEnumerable and IEnumerator is key to mastering C# collection iteration and LINQ.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!