Difficulty: Easy
Correct Answer: 1, 2, and 3 only
Explanation:
Introduction / Context:
In the .NET Framework, non-generic collections like ArrayList expose iteration capabilities via IEnumerable/IEnumerator. Understanding how enumerators are implemented, what nested classes can access, and what thread-safety guarantees exist helps developers avoid common bugs when iterating or modifying collections.
Given Data / Assumptions:
Concept / Approach:
ArrayList provides an enumerator implemented by a nested class (an inner type) that implements IEnumerator. In C#, a nested type has access to all members (including private) of its containing type. By default, ArrayList is not thread-safe, so simultaneous access from multiple threads requires external synchronization to be safe.
Step-by-Step Solution:
Verification / Alternative check:
Create an ArrayList, obtain IEnumerator via GetEnumerator(), and attempt to modify the collection during enumeration; observe InvalidOperationException. Inspect metadata (e.g., via decompilation) to see the nested enumerator type.
Why Other Options Are Wrong:
Common Pitfalls:
Assuming enumerators permit mutation; forgetting to synchronize or lock around shared ArrayList usage in multi-threaded code.
Final Answer:
1, 2, and 3 only
Discussion & Comments