This is in C#, I have a class that I am using from some else’s DLL. It does not implement IEnumerable but has 2 methods that pass back a IEnumerator. Is there a way I can use a foreach loop on these. The class I am using is sealed.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
foreachdoes not requireIEnumerable, contrary to popular belief. All it requires is a methodGetEnumeratorthat returns any object that has the methodMoveNextand the get-propertyCurrentwith the appropriate signatures./EDIT: In your case, however, you’re out of luck. You can trivially wrap your object, however, to make it enumerable:
/EDIT: The following method, proposed by several people, does not work if the method returns an
IEnumerator: