Yes, GetEnumerator() should work fine; although, strictly speaking, you need to handle Dispose() :
internal class WizardStepService : IDisposable { private IEnumerator<Question> _questions; public WizardStepService(Exam exam) { _questions = exam.Questions.GetEnumerator(); } public void Dispose() { if (_questions != null) _questions.Dispose(); } public Question GetNextQuestion() { if (_questions != null) { if (_questions.MoveNext()) { return _questions.Current; } Dispose();
as well as:
using (var wizardStepService = new WizardStepService(exam1)) { var question = wizardStepService.GetNextQuestion();
However, since you are not actually checking the result each time, you can also do something like:
using (var questions = exam1.Questions.GetEnumerator()) { questions.MoveNext(); var question = questions.Current;
Or as a final thought, maybe just:
var questions = exam1.Questions.Take(3).ToArray();
source share