C#.NET — ArrayList and IEnumerable/IEnumerator: identify the true statements about enumeration, thread-safety, and nested (inner) enumerator classes. Statements: 1) The ArrayList class contains a nested enumerator class that implements IEnumerator. 2) An ArrayList collection cannot be accessed safely by multiple threads simultaneously (it is not synchronized by default). 3) The nested enumerator class of ArrayList can access members of the outer ArrayList class. 4) To access members of ArrayList from the nested class, it is necessary to pass a reference of the outer ArrayList to it. 5) Enumerators of ArrayList can directly manipulate (modify) the underlying array.

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:

  • The collection under discussion is System.Collections.ArrayList (non-generic).
  • ArrayList implements IEnumerable and returns an IEnumerator from GetEnumerator().
  • We are considering default behaviors without applying wrappers like ArrayList.Synchronized or external locks.


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:

1) Statement 1: True. ArrayList uses a nested enumerator that implements IEnumerator and is returned by GetEnumerator().2) Statement 2: Interpreted correctly, this is True in the sense that ArrayList is not synchronized and therefore not safe for concurrent access without locks.3) Statement 3: True. A nested class can access members of its containing class directly; passing an explicit reference is not required.4) Statement 4: False. Due to nested-type access rules, no explicit reference passing is necessary to access the outer class’s members.5) Statement 5: False. Enumerators are intended for read-only iteration; modifying the collection during enumeration invalidates the enumerator and raises an exception.


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:

  • Options including statement 4 or 5 are incorrect because nested classes do not need an explicit reference, and enumerators should not modify the collection.


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

No comments yet. Be the first to comment!
Join Discussion