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 is generic version.

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!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: