Collections

SurveyBuilder Corner

In SurveyBuilder, our question and answer application, We make use of the collection type List<T> to store a collection of questions as part of a survey:

public class Survey
{
    public Survey(string name)
    {
        Name = name;
        Questions = new List<Question>();
    }

    public string Name { get; set; }
    public List<Question> Questions { get; set; }

    public Survey AddQuestion(Question question)
    {
        Questions.Add(question);
        return this;
    }

    public int GetScore(List<Answer> answers)
    {
        return answers.Sum(a => a.Points);
    }
}

We use the List<Question> type as this provides a simple API for managing a set of questions. We also accept a List<Answer> as a method argument for our GetScore method.

Page 29 of 31