Please login to save your progress.
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:
x
1
public class Survey
2
{
3
public Survey(string name)
4
{
5
Name = name;
6
Questions = new List<Question>();
7
}
8
9
public string Name { get; set; }
10
public List<Question> Questions { get; set; }
11
12
public Survey AddQuestion(Question question)
13
{
14
Questions.Add(question);
15
return this;
16
}
17
18
public int GetScore(List<Answer> answers)
19
{
20
return answers.Sum(a => a.Points);
21
}
22
}
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