向字典对象添加字符串[]

本文关键字:字符串 添加 字典 对象 | 更新日期: 2023-09-27 18:03:37

我正在尝试用c#创建一个测验。我试着在字典里添加问题…

这就是我走了多远…请帮助。

public class Quiz
{
    public string Question { get; set; }
    public string Answer { get; set; }
    public string ImagePath { get; set; }
    public string[] Options { get; set; }
    public Quiz(string question, string answer, string imagePath, string[] options)
    {
        Question = question;
        Answer = answer;
        ImagePath = imagePath;
        Options = options;
    }
    public static Dictionary<string, Quiz> GetQuiz() 
    {
        var quiz = new Dictionary<string, Quiz>();
        var questionOne = new Quiz("q1", "What is his first name?", "Elon", "/Assets/emusk.jpg", new string[]{"Peter","John","Mark"});
        quiz.Add("Q1", questionOne);
        return quiz;
    }   
}

向字典对象添加字符串[]

您还将"q1"添加到新的Quiz对象中。这应该只是你在字典中的键。

改为:

    var questionOne = new Quiz("What is his first name?", "Elon", "/Assets/emusk.jpg", new string[]{"Peter","John","Mark"});

有点奇怪,答案不在选项中!