C# 使用函数将值添加到列表

标签 c#

这里有一些背景: 我目前正忙于为学校作业做一个简单的学生管理。我目前正在添加将学生添加到类(class)的选项。我使用的代码是这样的:

class Student
    {
        //alle properties van de student
        public string studentNumber { get; set; } //student number
        public string name { get; set; } //name of the student
        public string adress { get; set; } //adress of the student
        public string phoneNumber { get; set; } //phone number og the student

        //a constructor
        public Student(string studentNumber, string name, string adress, string phoneNumber)
        {
            this.studentNumber= studentNumber;
            this.name= name;
            this.adress= adress;
            this.phoneNumber= phoneNumber;
        }

        //To string override
        public override string ToString()
        {
            return $"studentNumber: {studentNumber}, name: {name}, adress: {adress}, phoneNumber:{phoneNumber}";
            //"aantal vakken: { String.Join(", ", vakken)}"
        }

这代表学生的个人信息。我创建了以下类(class)的学生列表:

class Klas
{
    public string classCode{ get; set; }
    public string className{ get; set; }
    public List<Student> studentArray{ get; set; } //list with students

    public Klas(string klasnummer, string klasnaam, List<Student> studenten)
    {
        classCode= classCode;
        className= className;
        studentArray= studentArray;
    }
    static void AddStudents()
    {
        //this is where the students should be added to 'studentArray'
    }
}

现在我已经为学生可以放入的类(class)奠定了基础,但我仍然需要初始化这些类(class):

class Program
{
    
    static List<Klas> ClassList= new List<Klas>();
    public static void AllClass()
    {
        //list with klassen and students 
        ClassList.Add(new Klas("KL0001", "AO1-A", new List<Student>()
             {
                
             }
        ));

        ClassList.Add(new Klas("KL0002", "AO1-B", new List<Student>()
             {
               //this is where I used to create the students on the spot
             }
       ));

        ClassList.Add(new Klas("KL0003", "AO2-A", new List<Student>()
             {

             }
        ));

        ClassList.Add(new Klas("KL0004", "GD3-C", new List<Student>()
             {

             }
        ));

        ClassList.Add(new Klas("KL0176", "STM4-P", new List<Student>()
             {

             }
        ));
    }

    static void Main(string[] args)
    {
                   
    }

}

这个类(class)有几个学生需要加入的类(class)(或者作为学生不必参加类(class))。问题是我不允许简单地使用 new student("studentnummer", "naam", "all the other variabels you can see inside of the Student class") 添加这些 klassen 中的学生。我必须使用单独的函数( Voegstudenttoe() )来添加它们。

如果您认为这已经很多了,那么在我将学生添加到列表之前,这些学生必须已经存在,因为某些学生可能不在 klas 中,而是自己上学。

这一切最终导致我不知道该怎么做,因为我的小大脑无法理解这样的事情。换句话说:我需要帮助。

最佳答案

那个方法

static void Voegstudenttoe()
{
    //this is where the students should be added to a klas inside the klassenlist
}

不应该是静态的,因此您可以将 Student 添加到 Klas 的这个特定实例中。

接下来,您应该将要添加的学生作为参数传递。然后您可以轻松地将其添加到您的列表中:

public void Voegstudenttoe(Student newStudent)
{
   if (newStudent != null)
       this.Studentenlijst.Add(newStudent);
}

顺便说一下,Studentenlijst 属性也可以是:

public List<Student> Studentenlijst { get; } = new List<Student>();

这样就没有人可以替换该列表,并且您可以确定有一个列表(不为空)。

(在注释后添加)非空保证当然假设您不会替换构造函数中的列表(可能带有空值)。删除该参数或将所提供列表中的每个学生(请执行空检查)添加到现有内部列表中。

关于C# 使用函数将值添加到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59089949/

相关文章:

c# - 启用 https 时,应用程序设置中的连接字符串不起作用 : Azure Web App

c# - 在 Windows 8 应用程序中以编程方式使用 SSL 证书

c# - 获取添加到 Entity Framework 6 的对象列表包含列表

c# - 在 XNA 中钳制 TextureAddressMode

c# - 与 MahAppsMetro ProgressIndicator 绑定(bind)失败

c# - 如何以编程方式访问 EntityDataSource 选定的数据

c# - "is"运算符编译时警告

c# - UnityException : GetActiveScene is not allowed to be called from a MonoBehaviour constructor

c# - 在 Resharper 中忽略 "Cannot resolve symbol"

c# - javascript 未在 ASP.NET MVC 中调用