Theo Motsisi Theo Motsisi - 1 year ago 119
C# Question

Add a string[] to a Dictionary Object C#

Newbie Alert, i'm trying to create a Quiz in C#, tried adding questions into a dictionary...

This is how far i got..Please help

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;


Answer Source

You have added "q1" also into your new Quiz object. This should only be your key in the Dictionary.

Change it to:

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

A little odd, though, that the answer is not one of the options!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download