Choose the correct statements for the following C# code? [code] public System.Collections.IEnumerator GetEnumerator() { foreach

Pham Quỳnh

New member
Choose the correct statements for the following C# code?
Code:
 public System.Collections.IEnumerator GetEnumerator()
 {
     foreach (char ch in chrs)
     yield return ch;
 }
A. Definition of iterator for MyClass
B. Implements the GetEnumerator() method defined by IEnumerable
C. The yield return statement returns the next object in the collection, which in this case is the next character in chrs
D. All of the mentioned
 
Answer: d
Explanation: This is the definition of the iterator for MyClass. The code implicitly implements the GetEnumerator() method defined by IEnumerable. At the body of the method. It contains a foreach loop that returns the elements in chrs. It does this through the use of a yield return statement. The yield return statement returns the next object in the collection, which in this case is the next character in chrs.
 

Tìm kiếm

Back
Top