简体   繁体   English

如何填充C#中的类中的列表?

[英]How to populate a List that's within a class in C#?

I'm trying to understand nested types, but even after checking several sites I still don't get it. 我正在尝试了解嵌套类型,但是即使检查了几个站点,我仍然看不到它。 I have a List that's in a class. 我有一个在班上的清单。 I don't see how I can add new items to the List because when I try, it overwrites the existing items with my new ones. 我看不到如何将新项目添加到列表中,因为当我尝试时,它将用我的新项目覆盖现有项目。 Can someone please tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

class Program
{
    static void Main(string[] args)
    {
        Master master = new Master();
        master.Username = "Barry";

        Quiz quiz = new Quiz();
        quiz.QuizID = "quiz ID";
        master.quiz = quiz;

        Answers answers = new Answers();
        answers.Answer1 = "answer 1";
        answers.Answer2 = "answer 2";
        answers.Answer3 = "answer 3";
        master.quiz.answers.Add(answers);
        answers.Answer1 = "answer 11";
        answers.Answer2 = "answer 22";
        answers.Answer3 = "answer 33";
        master.quiz.answers.Add(answers);
    }
}

public class Answers
{
    public string Answer1 { get; set; }
    public string Answer2 { get; set; }
    public string Answer3 { get; set; }
}

public class Quiz
{
    public string QuizID { get; set; }
    public List<Answers> answers = new List<Answers>();
}

public class Master
{
    public string Username { get; set; }
    public Quiz quiz { get; set; }
}

When I do the second .Add and then check what's in master.quiz.answers it shows two entries, but they're both the 11, 22, 33 values as if the second .Add overwrote the data that was already in the list. 当我执行第二个.Add然后检查master.quiz.answers中的内容时,它显示了两个条目,但它们都是master.quiz.answers值,就像第二个.Add覆盖了列表中已经存在的数据一样。

Thanks. 谢谢。

You'll need to construct two distinct objects with the new operator. 您需要使用new运算符构造两个不同的对象。

ie

Answers answers = new Answers();
        answers.Answer1 = "answer 1";
        answers.Answer2 = "answer 2";
        answers.Answer3 = "answer 3";
        master.quiz.answers.Add(answers);

Answers anotherAnswer = new Answers(); // construct a new object here
        anotherAnswer.Answer1 = "answer 11";
        anotherAnswer.Answer2 = "answer 22";
        anotherAnswer.Answer3 = "answer 33";
        master.quiz.answers.Add(anotherAnswer);

As an aside, you can make this a little bit cleaner with object initializer syntax: 顺便说一句,您可以使用对象初始值设定项语法使此方法更简洁:

Answers answers = new Answers
{
      Answer1 = "answer 1",
      Answer2 = "answer 2",
      Answer3 = "answer 3"
};
master.quiz.answers.Add(answers);

Answers anotherAnswer = new Answers
{
      Answer1 = "answer 11",
      Answer2 = "answer 22",
      Answer3 = "answer 33"
}; 
master.quiz.answers.Add(anotherAnswer);

尝试再次实例化答案对象,然后将其第二次添加到测验中。

answers = new Answers();

Because you are updating the same object. 因为您要更新同一对象。 use 采用

 answers = new Answers();

after adding the object to the list 将对象添加到列表后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM