C# 将csv文件值存储在3个单独的列表中

C# 将csv文件值存储在3个单独的列表中,c#,C#,我创建了一个.csv文件,用于一个琐事游戏,其中存储了每个问题的问题、答案和1-3分。我想把问题存储在字符串列表中,答案存储在字符串列表中,分数存储在整数列表中。然后,我想一次一个地向用户显示每个问题、答案和分数。我不知道怎么做 任何帮助都将不胜感激 这是.csv文件代码 static void Main(string[] args) { // The path of the file to write to. string filename = "C:\\Int

我创建了一个
.csv
文件,用于一个琐事游戏,其中存储了每个问题的问题、答案和1-3分。我想把问题存储在字符串列表中,答案存储在字符串列表中,分数存储在整数列表中。然后,我想一次一个地向用户显示每个问题、答案和分数。我不知道怎么做

任何帮助都将不胜感激

这是
.csv
文件代码

static void Main(string[] args)
{
        // The path of the file to write to.
        string filename = "C:\\Intel\\Trivia.csv";

        // Parallel array of related trivia data
        string[] questions = { "Question1", "Question2", "Question3", "Question4", "Question5","Question6", "Question7","Question8","Question9","Question10", };
        string[] answers = { "A1", "A2", "A3", "A4", "A5", "A6", "A7","A8","A9","A10" };
        int[] points = { 1, 3, 3, 1, 2,1,2,1,3,1};

        try
        {
            // Write each element of the parallel array to a file.
            using (StreamWriter writer = new StreamWriter(filename))
            {
                for (int index = 0; index < questions.Length; index++)
                {
                    writer.WriteLine(questions[index] + ","
                        + answers[index] + ","

                        + points[index]
                        );
                }
                Console.WriteLine("Data successfully written to " + filename);
            }
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be written.");
            Console.WriteLine(e.Message);
        }

    }
}
static void Main(字符串[]args)
{
//要写入的文件的路径。
string filename=“C:\\Intel\\Trivia.csv”;
//相关琐事数据的并行数组
字符串[]问题={“问题1”、“问题2”、“问题3”、“问题4”、“问题5”、“问题6”、“问题7”、“问题8”、“问题9”、“问题10”、};
字符串[]答案={“A1”、“A2”、“A3”、“A4”、“A5”、“A6”、“A7”、“A8”、“A9”、“A10”};
int[]点={1,3,3,1,2,1,2,1,3,1};
尝试
{
//将并行数组的每个元素写入一个文件。
使用(StreamWriter=newstreamwriter(文件名))
{
for(int index=0;index
在我看来,这真的不是做事的方式。它可以工作,但非常脆弱,因为你必须处理三个数组,并保持它们完全同步。只创建一个类并保留一个对象数组要好得多:

void Main()
{
    // The path of the file to write to.
    string filename = "D:\\temp\\Trivia.txt";

    var questions = new List<QuestionInfo>() {
        new QuestionInfo("Q1", "A1", 1),
        new QuestionInfo("Q2", "A2", 3),
        new QuestionInfo("Q3", "A3", 3),
        new QuestionInfo("Q4", "A4", 1),
        new QuestionInfo("Q5", "A5", 2),
        new QuestionInfo("Q6", "A6", 1),
        new QuestionInfo("Q7", "A7", 2),
        new QuestionInfo("Q8", "A8", 1),
        new QuestionInfo("Q9", "A9", 3),
        new QuestionInfo("Q10", "A10", 1),
    };
    using (TextWriter tw = new StreamWriter(filename))
    {
        try
        {
            QuestionInfo.WriteCsv(questions, tw);
            Console.WriteLine("Data successfully written to " + filename);
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be written.");
            Console.WriteLine(e.Message);
        }
    }
}

public class QuestionInfo
{
    string Question { get; }
    string Answer { get; }
    int Points { get; }

    public QuestionInfo(string question, string answer, int points)
    {
        Question = question;
        Answer = answer;
        Points = points;
    }

    public void WriteCsv(TextWriter tw)
    {
        tw.WriteLine($"{Question},{Answer},{Points}");
    }

    public static void WriteCsv(List<QuestionInfo> questions, TextWriter tw)
    {
        foreach (var question in questions)
        {
            question.WriteCsv(tw);
        }
    }
}
void Main()
{
//要写入的文件的路径。
字符串filename=“D:\\temp\\Trivia.txt”;
var questions=新列表(){
新问题信息(“Q1”、“A1”、1),
新问题信息(“Q2”、“A2”和“3”),
新问题信息(“Q3”、“A3”和“3”),
新问题信息(“Q4”、“A4”和“1”),
新问题信息(“Q5”、“A5”、2),
新问题信息(“Q6”、“A6”、1),
新问题信息(“Q7”、“A7”、2),
新问题信息(“Q8”、“A8”、1),
新问题信息(“Q9”、“A9”、3),
新问题信息(“Q10”、“A10”、1),
};
使用(TextWriter tw=新StreamWriter(文件名))
{
尝试
{
问题信息。书面形式(问题,tw);
Console.WriteLine(“数据成功写入”+文件名);
}
捕获(例外e)
{
//让用户知道出了什么问题。
WriteLine(“无法写入文件”);
控制台写入线(e.Message);
}
}
}
公共类问题信息
{
字符串问题{get;}
字符串答案{get;}
int点{get;}
公共问题信息(字符串问题、字符串答案、整数点)
{
问题=问题;
答案=答案;
点数=点数;
}
公共无效WriteCsv(TextWriter tw)
{
tw.WriteLine($“{Question},{Answer},{Points}”);
}
公共静态无效WriteCsv(列出问题,TextWriter tw)
{
foreach(问题中的var问题)
{
问题.书面答覆(tw);
}
}
}

现在有了一个questioninfo对象数组,它将单个问题的所有信息保存在一个位置,而不是在三个数组中隔开。事实上,有很多NuGET软件包非常好地处理CSV文件,我肯定会考虑使用其中的一个,但是在这样的简单情况下自己处理它并不是什么大不了的事情。有快照吗?