Which criterion should the method meet?

You are developing a custom-collection class.
You need to create a method in your class.
You need to ensure that the method you create in your class returns a type that is compatible with the Foreach statement.
Which criterion should the method meet?

You are developing a custom-collection class.
You need to create a method in your class.
You need to ensure that the method you create in your class returns a type that is compatible with the Foreach statement.
Which criterion should the method meet?

A.
The method must return a type of either IEnumerator or IEnumerable.

B.
The method must return a type of IComparable.

C.
The method must explicitly contain a collection.

D.
The method must be the only iterator in the class.

Explanation:
Returning an IEnumerator will enable the ForEach statement. IEnumerable is a subtype of IEnumerator hence can also be up cast to IEnumerator.
IComparable is used to enable comparisons for a user type.
Explicitly containing a collection within the method will have no impact on the methods return type
which is what the ForEach statement will operate on.



Leave a Reply 1

Your email address will not be published. Required fields are marked *