gpt4 book ai didi

c# - IEnumerable - 移动到下一个

转载 作者:太空狗 更新时间:2023-10-29 22:59:55 26 4
gpt4 key购买 nike

我有一个 IEnumerable,我需要从中获取每个项目并一一显示。显示不是一个连续的过程……也就是说,我应该获取一个项目并将其显示在 UI 上,然后等待用户对该项目的一些反馈,然后继续进行下一个项目。例如,从下面的代码中,我需要获取一个问题,然后将其显示给用户,然后用户点击回车,然后我继续获取下一个问题。

我的问题是我该怎么做? IEnumerable 是实现此目标的最佳方法,还是我应该恢复为列表并开始存储索引并逐个递增?

请注意,我使用的是 .NET 3.5。

代码:

class Program
{
static void Main(string[] args)
{
Exam exam1 = new Exam()
{
Questions = new List<Question>
{
new Question("question1"),
new Question("question2"),
new Question("question3")
}
};
var wizardStepService = new WizardStepService(exam1);
var question = wizardStepService.GetNextQuestion();
//Should output question1
Console.WriteLine(question.Content);
Console.ReadLine();
//Should output question2 but outputs question1
question = wizardStepService.GetNextQuestion();
Console.WriteLine(question.Content);
Console.ReadLine();
//Should output question3 but outputs question1
question = wizardStepService.GetNextQuestion();
Console.WriteLine(question.Content);
Console.ReadLine();
}
}

public class Question
{
private readonly string _text;
public Question(string text)
{
_text = text;
}

public string Content { get { return _text; } }
}

internal class Exam
{
public IEnumerable<Question> Questions { get; set; }
}

internal class WizardStepService
{
private readonly Exam _exam;
public WizardStepService(Exam exam)
{
_exam = exam;
}

public Question GetNextQuestion()
{
foreach (var question in _exam.Questions)
{
//This always returns the first item.How do I navigate to next
//item when GetNextQuestion is called the second time?
return question;
}

//should have a return type hence this or else not required.
return null;
}
}

最佳答案

是的,GetEnumerator() 应该可以正常工作;尽管严格来说您需要处理 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(); // no more questions!
}

//should have a return type hence this or else not required.
return null;
}
}

还有:

using (var wizardStepService = new WizardStepService(exam1))
{
var question = wizardStepService.GetNextQuestion();
//Should output question1
Console.WriteLine(question.Content);
Console.ReadLine();
//Should output question2 but outputs question1
question = wizardStepService.GetNextQuestion();
Console.WriteLine(question.Content);
Console.ReadLine();
//Should output question3 but outputs question1
question = wizardStepService.GetNextQuestion();
Console.WriteLine(question.Content);
Console.ReadLine();
}

但是,由于您并不是每次都实际测试结果,您还可以执行以下操作:

using (var questions = exam1.Questions.GetEnumerator())
{
questions.MoveNext();
var question = questions.Current;
//Should output question1
Console.WriteLine(question.Content);
Console.ReadLine();
//Should output question2 but outputs question1
questions.MoveNext();
question = questions.Current;
Console.WriteLine(question.Content);
Console.ReadLine();
//Should output question3 but outputs question1
questions.MoveNext();
question = questions.Current;
Console.WriteLine(question.Content);
Console.ReadLine();
}

或者作为最后的想法,也许只是:

var questions = exam1.Questions.Take(3).ToArray();

//Should output question1
Console.WriteLine(questions[0].Content);
Console.ReadLine();
//Should output question2 but outputs question1
Console.WriteLine(questions[1].Content);
Console.ReadLine();
//Should output question3 but outputs question1
Console.WriteLine(questions[2].Content);
Console.ReadLine();

关于c# - IEnumerable - 移动到下一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18231749/

26 4 0