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

Learn More About C# ๐Ÿ“š

1. What is C#? ๐Ÿ‘‰ Explained
2. Main Features of C# ๐Ÿ‘‰ Explained
3. Difference Between C# and Java ๐Ÿ‘‰ Explained
4. Common Language Runtime (CLR) in C# ๐Ÿ‘‰ Explained
5. Common Type System (CTS) in C# ๐Ÿ‘‰ Explained
6. Common Language Specification (CLS) in C# ๐Ÿ‘‰ Explained
7. Value Types vs Reference Types in C# ๐Ÿ‘‰ Explained
8. What is a Namespace in C#? ๐Ÿ‘‰ Explained
9. Purpose of the 'using' Keyword in C# ๐Ÿ‘‰ Explained
10. Different Data Types in C# ๐Ÿ‘‰ Explained
11. Difference Between int and Int32 in C# ๐Ÿ‘‰ Explained
12. Difference Between float, double, and decimal in C# ๐Ÿ‘‰ Explained
13. What is the Default Value of a Boolean in C#? ๐Ÿ‘‰ Explained
14. What is Boxing and Unboxing in C# ๐Ÿ‘‰ Explained
15. What are the Different Types of Operators in C# ๐Ÿ‘‰ Explained
16. Difference Between Equals and == in C# ๐Ÿ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# ๐Ÿ‘‰ Explained
18. What is the Ternary Operator in C# ๐Ÿ‘‰ Explained
19. How Does the Switch Statement Work in C# ๐Ÿ‘‰ Explained
20. What is Object-Oriented Programming in C# ๐Ÿ‘‰ Explained
21. What are the Four Pillars of OOP in C# ๐Ÿ‘‰ Explained
22. What is Encapsulation in C# ๐Ÿ‘‰ Explained
23. What is Inheritance in C# ๐Ÿ‘‰ Explained
24. What is Polymorphism in C# ๐Ÿ‘‰ Explained
25. What is Abstraction in C# ๐Ÿ‘‰ Explained
26. What is an Abstract Class in C# ๐Ÿ‘‰ Explained
27. What is an Interface in C# ๐Ÿ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? ๐Ÿ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# ๐Ÿ‘‰ Explained
30. How Do You Create a Class in C#? ๐Ÿ‘‰ Explained
31. What is a Constructor in C# ๐Ÿ‘‰ Explained
32. What Are the Types of Constructors in C# ๐Ÿ‘‰ Explained
33. What is a Static Constructor in C# ๐Ÿ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# ๐Ÿ‘‰ Explained
35. What is the Use of 'this' Keyword in C# ๐Ÿ‘‰ Explained
36. What is a Destructor in C# ๐Ÿ‘‰ Explained
37. What is Object Initializer Syntax in C# ๐Ÿ‘‰ Explained
38. What is the Difference Between Field and Property in C# ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup