How to delete an item from a generic list

后端 未结 9 565
死守一世寂寞
死守一世寂寞 2020-12-31 05:51

I have a generic list

How do I remove an item?

EX:

Class Student
{
    private number;
    public Number
    {
        get( return number;)
          


        
9条回答
  •  长情又很酷
    2020-12-31 06:26

    List students = new List();
    students.Add(new Student {StudentId = 1, StudentName = "Bob",});
    students.RemoveAt(0); //Removes the 1st element, will crash if there are no entries
    

    OR to remove a known Student.

    //Find a Single Student in the List.
    Student s = students.Where(s => s.StudentId == myStudentId).Single();
    //Remove that student from the list.
    students.Remove(s);
    

提交回复
热议问题