c# - 不知道使用哪种类型的数组 C#
问题描述
我需要创建一个包含 10 个问题和每个问题 3 个答案的小测验。所以我有一个带有问题的数组和一个带有正确答案的数组。但我不知道要为所有答案使用哪个数组(正确的和不正确的)。注意:我会打乱每个数组以随机化所有内容。
解决方案
没有开箱即用的数组类型可以满足您的要求。您可以创建一些存储和处理测验数据的类型。例子:
public class Answer
{
public bool IsTrue { get; set; } = false;
public string Text { get; set; } = string.Empty;
}
public class Question
{
public string Text { get; set; } = string.Empty;
public List<Answer> Answers { get; set; } = new List<Answer>();
}
public class Quiz
{
public List<Question> Questions { get; set; } = new List<Question>();
}
var question1 = new Question()
{
Text = "When should I use an array instead of List<T>?",
Answers = new List<Answer>
{
new Answer() {IsTrue = true, Text = "If you don't know what a generic is."},
new Answer() {IsTrue = false, Text = "If you want to save tons of memory."},
new Answer() {IsTrue = false, Text = "If you want faster code."},
}
};
var quiz = new Quiz();
quiz.Questions.Add(question1);
推荐阅读
- assembly - 为什么我们必须在这里取消引用标准输出?
- r - R 无法正确使用 UTF-8 编码罗马字符
- kubernetes - 如何查看部署在 GKE 中的应用的访问日志?
- blogger - 调整浏览器窗口大小时,我希望侧边栏在右下角之间移动
- javascript - 使用 FormData 的 Ajax Post 永远不会出错或成功
- python - 如何展平深度嵌套的元组?
- python - 生成树的定义
- javascript - 下载在 firefox & IE & Edge 中不起作用(扩展问题)
- html - Outlook 中的 HTML 电子邮件预览(最新版本)
- cloudify - 如何使用 Cloudify REST 插件发送八位字节流内容类型