我有一个列表,其中包含具有名称、姓氏、id、年份等属性的 Student 类型的对象。我想要的是从具有特定索引的列表中删除特定的学生并将其放入另一个列表中。有没有办法这样做?例如:
if (((mystudent[index].Year== 7)) {
mystudent.RemoveAt(index);
// now how I shall throw the Student to a new list
}
感谢您的信息。我现在的问题是我在单独的类中有 2 个列表。当我从列表中删除一个项目时,它的工作但当我想从其他类查看添加到列表中的项目时,我没有得到该项目。
public class SeniorStudentsClass
{
public List<Student> studlist = new List<Student>();
public void ViewStudents()
{
for (int i = 0; i < studlist.Count; i++)
{
Console.Write(studlist[i].Id + "\t");
Console.Write(studlist[i].Year + "\t");
Console.Write(studlist[i].Name + "\t");
Console.Write(studlist[i].Surname + "\t");
Console.Write(studlist[i].DOB + "\t");
Console.Write(studlist[i].Addr);
Console.WriteLine();
}
public class JuniorStudentsClass
{
public List<Student> mystudent = new List<Student>();
SeniorStudentsClass sc = new SeniorStudentsClass();
public void PromoteStudents(int index)
{
SearchItem(index);
Console.WriteLine("current record:");
Console.WriteLine("id is:" + mystudent[index].Id);
Console.WriteLine("year is:" + mystudent[index].Year);
Console.WriteLine("name is:" + mystudent[index].Name);
Console.WriteLine("surname is:" + mystudent[index].Surname);
Console.WriteLine("dob is:" + mystudent[index].DOB);
Console.WriteLine("address is:" + mystudent[index].Addr);
Console.WriteLine("year is:"+mystudent[index].Year);
if (((mystudent[index].Year== 7)) || ((mystudent[index].Year == 8)))
{
var student = mystudent[index];
mystudent.RemoveAt(index);
sc.studlist.Add(student);
Console.WriteLine("student promoted to senior student");
}