C#将一个对象分配给一个对象列表

C#将一个对象分配给一个对象列表,c#,C#,哦,孩子,我又来了 我目前正在从事我的一个小型C#项目,以增加我在编码方面的知识和技能 我现在有一个学生班,它保存着一个名为“学生”的学生个人信息,还有一个名为“klas”的班级(就像一个满是学生的班级)。 在“klas”里面,我列出了一个学生(对象)列表。此外,我还列出了几个“Klass”(对象),尽管我不知道这是否是个好主意 class Student { //alle properties van de student public string studentnummer

哦,孩子,我又来了

我目前正在从事我的一个小型C#项目,以增加我在编码方面的知识和技能

我现在有一个学生班,它保存着一个名为“学生”的学生个人信息,还有一个名为“klas”的班级(就像一个满是学生的班级)。 在“klas”里面,我列出了一个学生(对象)列表。此外,我还列出了几个“Klass”(对象),尽管我不知道这是否是个好主意

class Student
{
    //alle properties van de student
    public string studentnummer { get; set; }
    public string naam { get; set; }
    public string postcode { get; set; }
    public string telefoonnummer { get; set; }
    //public List<string> vakken { get; set; } = new List<string>();

    //een no-argument constructor
    public Student(string studentnummer1, string naam1, string postcode1, string telefoonnummer1)
    {
        studentnummer = studentnummer1;
        naam = naam1;
        postcode = postcode1;
        telefoonnummer = telefoonnummer1;
    }
 }
班级学生
{
//凡德学生酒店
公共字符串studentnummer{get;set;}
公共字符串naam{get;set;}
公共字符串邮政编码{get;set;}
公共字符串telefoonnummer{get;set;}
//public List vakken{get;set;}=new List();
//甚至没有参数构造函数
公立学生(字符串studentnummer1、字符串naam1、字符串邮政编码1、字符串Telefoonnumer1)
{
studentnummer=studentnummer1;
naam=naam1;
邮政编码=邮政编码1;
telefoonnummer=telefoonnummer1;
}
}
这是“klas”课程

 class Klas
{
    public string klascode { get; set; }
    public string klasnaam { get; set; }

    public Klas(string klasnummer1, string klasnaam1)
    {
        klascode = klasnummer1; 
        klasnaam = klasnaam1;    
    }

    //maakt nieuwe studenten aan in een list (experimenteel)-(niet final)
    //problemen die ik hier mee heb: hoe ga ik individuele studenten aan een lijst toevoegen?, 
    public void studentenlijst()
    {
        var studentlijst = new List<Student>()
        {
            new Student("S101010", "Voornaam - Achternaam", "1234AT", "+06 0571 261442"),                                               //student A
            new Student("S202020", "Voornaamer - Achternaamer", "2345AT", "+06 0571 372553"),                                           //student B
            new Student("S303030", "Voornaamste - Achternaamste", "3456AT", "+06 0571 483664"),                                         //student C
            new Student("S404040", "Voorgenaamste - Achtergenaamste", "4567AT", "+06 0571 594775"),                                     //student D
            new Student("S505050", "Voorgenaamst've - Achtergenaamste've", "5678AT", "+06 0571 605886"),                                //student E
            new Student("S606060", "Voorgenaamste'ntve - Achtergenaamste'ntve", "6789AT", "+06 0571 716997"),                           //student F
            new Student("S707070", "Voorgenaamste'ntve'se - Achtergenaamste'ntve'se", "7890AT", "+06 0571 827008"),                     //student G
            new Student("S808080", "Voorgenaamste'ntve'se'lu - Achtergenaamste'ntve'se'lu", "8901AT", "+06 0571 938119"),               //student H
            new Student("S909090", "Voorgenaamste'ntve'se'lu'yiant - Achtergenaamste'ntve'se'lu'yiant", "9012AT", "+06 0571 049220"),   //student I
            new Student("S010101", "You thought it was a normal name - BUT IT WAS ME, DIO!", "0123AT", "+06 0571 150331")               //student JOJO!
        };
    }

    public void klassenlijst()
    {
        //maak meerdere lists(klassen) en zet daar je studenten in.
        var klassenlijst = new List<Klas>
        {
            new Klas("KL0001", "AO1-A"),    //klas 1
            new Klas("KL0002", "AO1-B"),    //klas 2
            new Klas("KL0003", "AO2-A"),    //klas 3
            new Klas("KL0004", "AO2-B"),    //klas 4
            new Klas("KL0005", "AO3-A"),    //klas 5
            new Klas("KL0006", "GD1-B"),    //Klas 6
            new Klas("KL0325", "STM1-D"),   //klas 7
        };
    }
Klas类
{
公共字符串klascode{get;set;}
公共字符串klasnaam{get;set;}
公共Klas(字符串Klasummer1、字符串klasnaam1)
{
klascode=Klasummer1;
klasnaam=klasnaam1;
}
//在een列表中的maakt nieuwe studenten aan(实验)-(niet最终版)
//有问题的人会问:每个学生都有哪些问题?,
公共无效学生人数()
{
var studentlijst=新列表()
{
新学生(“S101010”、“Voornaam-Achternaam”、“1234AT”、“+060571261442”),//学生A
新学生(“S202020”、“沃纳默-阿赫特纳默”、“2345AT”、“+06 0571 372553”),//学生B
新学生(“S303030”、“Voornaamste-Achternaamste”、“3456AT”、“+060571483664”),//学生C
新学生(“S404040”、“沃根那阿姆斯特-阿赫特根那阿姆斯特”、“4567AT”、“+060571594775”),//学生D
新生(“S505050”、“Voorgenaamst've-Achtergenaamst've”、“5678AT”、“+060571605886”),//学生E
新学生(“S606060”、“Voorgenamster'ntve-Achtergenamster'ntve”、“6789AT”、“+0605716997”),//学生F
新学生(“S707070”、“Voorgenamster'ntve'se-Achtergenamster'ntve'se”、“7890AT”、“+060571827008”),//学生G
新学生(“S808080”,“Voorgenamster'ntve'se'lu-Achtergenamster'ntve'se'lu”,“8901AT”,“+060571938119”),//学生H
新学生(“S909090”、“沃根南斯特”恩特”恩特”恩特”恩特”恩特”恩特”恩特”恩特”恩特”恩特“恩特”恩特”恩特”恩特”恩特“恩特”恩特”恩特“恩特”恩特”恩特“恩特”恩特”恩特”恩特“恩特”恩特”恩特“恩特”恩特”恩特”恩特“恩
新生(“S010101”,“你以为这是一个普通的名字-但那是我,迪奥!”,“0123AT”,“+060571150331”)//Student JOJO!
};
}
公共无效klassenlijst()
{
//maak meerdere名单(klassen)上的所有学生。
var klassenlijst=新列表
{
新Klas(“KL0001”,“AO1-A”),//Klas 1
新Klas(“KL0002”,“AO1-B”),//Klas 2
新Klas(“KL0003”,“AO2-A”),//Klas 3
新Klas(“KL0004”,“AO2-B”),//Klas 4
新Klas(“KL0005”,“AO3-A”),//Klas 5
新Klas(“KL0006”,“GD1-B”),//Klas 6
新Klas(“KL0325”,“STM1-D”),//Klas 7
};
}
我想做的是: 我希望能够将某些学生放入某些klassen中,并使用一种方法调用/打印这些klassen

我只是不知道从哪里开始,更不用说我如何才能做到这一点,如果可能的话


有人想给我一些建议和/或建议一种替代方法来完成这项工作吗?

你可以尝试下面的方法

  class Program
{
    static List<Klas> klasList = new List<Klas>();
    static void Main(string[] args)
    {
        klasList.Add(new Klas("KL0001", "AO1-A", new List<Student>()
                                                  {
                                                    new Student("S101010", "Voornaam - Achternaam", "1234AT", "+06 0571 261442"),       //student A
                                                    new Student("S202020", "Voornaamer - Achternaamer", "2345AT", "+06 0571 372553"),    //student B
                                                    new Student("S303030", "Voornaamste - Achternaamste", "3456AT", "+06 0571 483664")
                                                  }
        ));

        klasList.Add(new Klas("KL0002", "AO1-B", new List<Student>()
                                                 {
                                                    new Student("S707070", "Voorgenaamste'ntve'se - Achtergenaamste'ntve'se", "7890AT", "+06 0571 827008"),                     //student G
                                                    new Student("S808080", "Voorgenaamste'ntve'se'lu - Achtergenaamste'ntve'se'lu", "8901AT", "+06 0571 938119"),               //student H
                                                    new Student("S909090", "Voorgenaamste'ntve'se'lu'yiant - Achtergenaamste'ntve'se'lu'yiant", "9012AT", "+06 0571 049220"),   //student I
                                                    new Student("S010101", "You thought it was a normal name - BUT IT WAS ME, DIO!", "0123AT", "+06 0571 150331")               //student JOJO!
                                                 }
       ));

        foreach (var klas in klasList)
        {

            Console.WriteLine(string.Format("Klasnaam: {0} - Klascode: {1}", klas.Klasnaam, klas.Klascode));
            foreach (var student in klas.Students)
            {
                Console.WriteLine(string.Format("Studentnummer: {0} - Naam: {1}", student.Studentnummer, student.Naam));
            }
        }
        Console.ReadKey();
    }
}   

你可以试试下面的方法

  class Program
{
    static List<Klas> klasList = new List<Klas>();
    static void Main(string[] args)
    {
        klasList.Add(new Klas("KL0001", "AO1-A", new List<Student>()
                                                  {
                                                    new Student("S101010", "Voornaam - Achternaam", "1234AT", "+06 0571 261442"),       //student A
                                                    new Student("S202020", "Voornaamer - Achternaamer", "2345AT", "+06 0571 372553"),    //student B
                                                    new Student("S303030", "Voornaamste - Achternaamste", "3456AT", "+06 0571 483664")
                                                  }
        ));

        klasList.Add(new Klas("KL0002", "AO1-B", new List<Student>()
                                                 {
                                                    new Student("S707070", "Voorgenaamste'ntve'se - Achtergenaamste'ntve'se", "7890AT", "+06 0571 827008"),                     //student G
                                                    new Student("S808080", "Voorgenaamste'ntve'se'lu - Achtergenaamste'ntve'se'lu", "8901AT", "+06 0571 938119"),               //student H
                                                    new Student("S909090", "Voorgenaamste'ntve'se'lu'yiant - Achtergenaamste'ntve'se'lu'yiant", "9012AT", "+06 0571 049220"),   //student I
                                                    new Student("S010101", "You thought it was a normal name - BUT IT WAS ME, DIO!", "0123AT", "+06 0571 150331")               //student JOJO!
                                                 }
       ));

        foreach (var klas in klasList)
        {

            Console.WriteLine(string.Format("Klasnaam: {0} - Klascode: {1}", klas.Klasnaam, klas.Klascode));
            foreach (var student in klas.Students)
            {
                Console.WriteLine(string.Format("Studentnummer: {0} - Naam: {1}", student.Studentnummer, student.Naam));
            }
        }
        Console.ReadKey();
    }
}   

@Fahad的答案是完全正确的,但我认为它缺少一些解释。我将接受他的答案,并进一步解释他的更改。你可能需要,也可能不需要,想象一下我试图向自己解释答案。当我第一次开始时,我很难理解其中的一些概念。我实际上并没有运行所有这些代码,t这里可能有一些怪癖,但这是关于这个想法

class Klas
{
    // these properties are pretty self-explanatory
    public string Klascode { get; set; }
    public string Klasnaam { get; set; }
    // a Klas has a List of Students
    // I changed List to IList (i.e., an interface rather than a class)
    // for this example it doesn't matter.
    // however, when you get to the point of creating your own interfaces
    // there *can* be a benefit to it (not going to explain now).
    public IList<Student> Students { get; set; }

    // A constructor allows you to set the values of an object at the time of creation.
    // In fact, in this case, you *have* to supply all 3 properties
    // the take-away here is that this is not required.
    public Klas(string klasnummer, string klasnaam, List<Student> students)
    {
        Klascode = klasnummer;
        Klasnaam = klasnaam;
        Students = students;
    }
}

// let's show some stuff
public static void Main(string[] args)
{
    // You already kow how to create students and how to assign them to a list immediately
    IList<Student> studentlijst = new List<Student>()
    {
        new Student("S101010", "Voornaam - Achternaam", "0000xx", "+06 012345678"),
        new Student("S202020", "Voornaamer - Achternaamer", "0000xx", "+06 1234 5678")
    };
    // let's create a single Klas
    Klas KlasA = new Klas("A0", "Eerste klas", studentenlijst);
    // that gives you a KlasA of type Klas, with all info
    // let's list the students:
    foreach (var student in KlasA.Students)
    {
        Console.WriteLine(string.Format("Studentnummer: {0} - Naam: {1}", student.Studentnummer, student.Naam));
    }

    // But wait! You want to add another student.
    // In this case you could simply do:
    KlasA.Students.Add(new Student("0123", "zwarte", "piet", "+34 123 456 789"));
}
让我们看看实际情况:

public static void Main(string[] args)
{
    // let's create a single Klas
    Klas klasA = new Klas();
    klasA.Klascode = "B0";
    klasA.Klasnaam = "lekker boeie";
    // not that we did not set the Students property

    // let's list the students:
    foreach (var student in klasA.Students)
    {
        // Oops. This crashes. Will throw a Null reference exception
    }
}
Klas
类仍然有一个非常好的
Students
属性。但我们从未设置过它。因此它仍然是
null
。我几乎可以保证,这将在将来绊倒你。当你第一次遇到这个问题时,你会像“到底发生了什么事?”一样。在将来的某个时候,你会编写这样的类不在构造函数中填充它们的所有属性。它们将具有默认值。
String
将为空,
int
将为0,对象(如
List
)将为空。然后,您可能需要探索“支持字段”的概念和类似的神秘术语(如适用)

我喜欢将对象(类、类型)视为乐高积木,当你是一个初学者时,行话就不那么重要了——你会明白的,因为你认为它们是有意义的。你可以简单地把它们和.Net已经包含的所有乐高积木放在一起

好吧,这确实是我对我的一次谈话,但我希望它提供了一些额外的信息


享受编码吧!

@Fahad的答案完全正确,但我认为它缺乏一些解释。我将接受他的答案,并进一步解释他的变化。你可能需要,也可能不需要,想象一下我试图向自己解释答案。当我第一次开始时,我很难掌握其中一些概念。实际上,我没有运行所有这些代码,可能会有一些怪癖,但这与想法有关

class Klas
{
    // these properties are pretty self-explanatory
    public string Klascode { get; set; }
    public string Klasnaam { get; set; }
    // a Klas has a List of Students
    // I changed List to IList (i.e., an interface rather than a class)
    // for this example it doesn't matter.
    // however, when you get to the point of creating your own interfaces
    // there *can* be a benefit to it (not going to explain now).
    public IList<Student> Students { get; set; }

    // A constructor allows you to set the values of an object at the time of creation.
    // In fact, in this case, you *have* to supply all 3 properties
    // the take-away here is that this is not required.
    public Klas(string klasnummer, string klasnaam, List<Student> students)
    {
        Klascode = klasnummer;
        Klasnaam = klasnaam;
        Students = students;
    }
}

// let's show some stuff
public static void Main(string[] args)
{
    // You already kow how to create students and how to assign them to a list immediately
    IList<Student> studentlijst = new List<Student>()
    {
        new Student("S101010", "Voornaam - Achternaam", "0000xx", "+06 012345678"),
        new Student("S202020", "Voornaamer - Achternaamer", "0000xx", "+06 1234 5678")
    };
    // let's create a single Klas
    Klas KlasA = new Klas("A0", "Eerste klas", studentenlijst);
    // that gives you a KlasA of type Klas, with all info
    // let's list the students:
    foreach (var student in KlasA.Students)
    {
        Console.WriteLine(string.Format("Studentnummer: {0} - Naam: {1}", student.Studentnummer, student.Naam));
    }

    // But wait! You want to add another student.
    // In this case you could simply do:
    KlasA.Students.Add(new Student("0123", "zwarte", "piet", "+34 123 456 789"));
}
让我们看看实际情况:

public static void Main(string[] args)
{
    // let's create a single Klas
    Klas klasA = new Klas();
    klasA.Klascode = "B0";
    klasA.Klasnaam = "lekker boeie";
    // not that we did not set the Students property

    // let's list the students:
    foreach (var student in klasA.Students)
    {
        // Oops. This crashes. Will throw a Null reference exception
    }
}