Вы смотрите не на ту проблему. Вместо многомерного массива (что-то довольно редко используется из-за малой поддержки) используйте зубчатый массив.
string[][] questions = new[] {
new [] {"What is the capital of France", "Paris"},
new [] {"What is the capital of Spain", "Madrid"},
new [] {"What is the captial of Russia", "Moscow"},
new [] {"What is the capital of Ukraine", "Kiev"},
};
// use: questions[0][0] (question), questions[0][1] (answer), questions[1][0] (question)...
или (лучше) создать класс с двумя членами, Question
и Answer
.
class QuestionAndAnswer
{
public string Question { get; protected set; }
public string Answer { get; protected set; }
public QuestionAndAnswer(string question, string answer)
{
this.Question = question;
this.Answer = answer;
}
}
QuestionAndAnswer[] questions = new QuestionAndAnswer[] {
new QuestionAndAnswer("What is the capital of France", "Paris"),
new QuestionAndAnswer("What is the capital of Spain", "Madrid"),
// ...
};
// use: questions[0].Question, questions[0].Answer...
Вы можете использовать алгоритм Knuth :-)
Цитата оттуда:
To shuffle an array a of n elements (indexes 0..n-1):
for i from n − 1 downto 1 do
j ← random integer with 0 ≤ j ≤ i
exchange a[j] and a[i]
В C # алгоритм будет выглядеть примерно так:
Random rnd = new Random();
for (int i = questions.Length - 1; i >= 1; i--)
{
// Random.Next generates numbers between min and max - 1 value, so we have to balance this
int j = rnd.Next(0, i + 1);
if (i != j)
{
var temp = questions[i];
questions[i] = questions[j];
questions[j] = temp;
}
}