For Deleting Object from a list, Remove method must be used.
Remove Method deletes the value to be deleted. If the value to be deleted is more than one in the list, it removes the first value. This method is often used to remove values with reference types. But it can also be used with value types.
Following example we creates a student class. Then we create a list in Main method and fill it some student object. Then for instance we find some students by name, we delete them in linq with remove method.
In solution 1, problem solved by Linq, in solution 2 problem solved by decreasing for loop and COUNT, in solution 3 problem solved by ascending for loop and CAPACITY. In solution 2 and 3 you can remove object while iterating Collection in C#
Student Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Student { string name; int age; public Student(string name,int age) { Name = name; Age = age; } public string Name { get => name; set => name = value; } public int Age { get => age; set => age = value; } } |
Solution 1: Main Method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Program { static void Main(string[] args) { Console.WriteLine("...::Remove Object From a List in C#::... \n"); List<Student> mylist = new List<Student>(); mylist.Add(new Student("PAUL", 40)); //x mylist.Add(new Student("JAMES",12)); mylist.Add(new Student("DAVID",13)); mylist.Add(new Student("PAUL",25)); //x mylist.Add(new Student("GEORGE",17)); mylist.Add(new Student("LARRY",18)); mylist.Add(new Student("PAUL", 20)); //x mylist.Add(new Student("DAVID", 33)); int itemCount = mylist.Count; Console.WriteLine("count of items before deleting:{0}",mylist.Count); string deleteName = "PAUL"; mylist.RemoveAll(x => x.Name == deleteName); Console.WriteLine("count of items after deleting:{0}", mylist.Count); Console.ReadLine(); } } |
Solution 2: Main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
class Program { static void Main(string[] args) { Console.WriteLine("...::Remove Object From a List in C#::... \n"); List<Student> mylist = new List<Student>(); mylist.Add(new Student("PAUL", 40)); //x mylist.Add(new Student("JAMES",12)); mylist.Add(new Student("DAVID",13)); mylist.Add(new Student("PAUL",25)); //x mylist.Add(new Student("GEORGE",17)); mylist.Add(new Student("LARRY",18)); mylist.Add(new Student("PAUL", 20)); //x mylist.Add(new Student("DAVID", 33)); int itemCount = mylist.Count; Console.WriteLine("count of items before deleting:{0}",mylist.Count); string deleteName = "PAUL"; for (int i = itemCount - 1; i >= 0; i--) { if (mylist[i].Name == deleteName) { mylist.Remove(mylist[i]); //mylist.RemoveAt(i); } } Console.WriteLine("count of items after deleting:{0}", mylist.Count); Console.ReadLine(); } } |
Solution 3 Main: If you want to for loop by increment value, use CAPACITY instead of COUNT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
class Program { static void Main(string[] args) { Console.WriteLine("...::Remove Object From a List in C#::... \n"); List<Student> mylist = new List<Student>(); mylist.Add(new Student("PAUL", 40)); //x mylist.Add(new Student("JAMES",12)); mylist.Add(new Student("DAVID",13)); mylist.Add(new Student("PAUL",25)); //x mylist.Add(new Student("GEORGE",17)); mylist.Add(new Student("LARRY",18)); mylist.Add(new Student("PAUL", 20)); //x mylist.Add(new Student("DAVID", 33)); Console.WriteLine("count of items before deleting:{0}",mylist.Count); string deleteName = "PAUL"; for (int i = 0; i < mylist.Capacity; i++) // COUNT changes depending on deleteted item. You should use Capacity { if (mylist[i].Name == deleteName) { mylist.Remove(mylist[i]); } } Console.WriteLine("count of items after deleting:{0}", mylist.Count); Console.ReadLine(); } } |
Output: