C# 我如何为类批创建一个通用列表,并在类批内部放置学生类信息

C# 我如何为类批创建一个通用列表,并在类批内部放置学生类信息,c#,C#,在这里,我想在批处理类中使用student类,并使用List类使用它们,如何为List创建这两个类的对象 例如,如果我想从批次中找到学生的姓名,我该如何做 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Challenge { class student { string student_name;

在这里,我想在批处理类中使用student类,并使用List类使用它们,如何为List创建这两个类的对象

例如,如果我想从批次中找到学生的姓名,我该如何做

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace Challenge
 {
     class student
    {
        string student_name;
       int age, rollnumber;

       public student(string student_name, int age, int rollnumber)
      {
            this.age = age;
            this.student_name = student_name;
            this.rollnumber = rollnumber;


       }
     }

  class batch
  {
     string batch_name;
     int fees, duration;

      public batch(string batch_name,int fees,int duration)
      {
        this.batch_name = batch_name;
        this.fees = fees;
        this.duration = duration;


     }
 }

  class Program
 {
      static void Main(string[] args)
      {
         List<batch> obj = new List<batch>();

      }
  }  
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间挑战
{
班级学生
{
学生姓名字符串;
int age,rollnumber;
公立学生(字符串学生名称、整数年龄、整数编号)
{
这个。年龄=年龄;
this.student\u name=student\u name;
this.rollnumber=rollnumber;
}
}
类批
{
字符串批处理名称;
费用、期限;
公共批次(字符串批次名称、整数费用、整数持续时间)
{
this.batch\u name=批次名称;
此项费用=费用;
这个。持续时间=持续时间;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
List obj=新列表();
}
}  
}

您想做什么还不是很清楚,但我会尽力帮助您。据我所知,您希望创建一个
Batch
es集合,其中每个
Batch
都包含一个
Student
s集合

批处理
类中声明
列表
属性:

public class Batch
{
    public string Name { get; set; }
    public int Fee { get; set; }
    public int Duration { get; set; }
    public List<Student> Students { get; set; }
}

public class Student
{
    public string Name { get; set; }
    public int RollNumber { get; set; }
    // Date of birth instead of age, as explained in comment by Dour High Arch.
    public DateTime DateOfBirth { get; set; } 
}

在该代码中,
student
不知道
batch
,反之亦然。也没有任何列表的迹象,请存储生日而不是年龄;年龄不断变化,出生日期不变。Console.WriteLine($“Student:{Student.Name}”);
        var batches = new List<Batch>
        {
            new Batch
            {
                Name = "Batch 1",
                Duration = 100,
                Fee = 200,
                Students = new List<Student>
                {
                    new Student
                    {
                        Name = "Student 1",
                        DateOfBirth = DateTime.Today,
                        RollNumber = 1
                    },
                    new Student
                    {
                        Name = "Student 2",
                        DateOfBirth = DateTime.Today,
                        RollNumber = 2
                    }
                }
            }
        };
foreach (var batch in batches)
{
    Console.WriteLine($"Batch: {batch.Name}");
    foreach (var student in batch.Students)
    {
        Console.WriteLine($"Student: {student.Name}");
    } 
}