Shubh Shubh - 4 years ago 137
C# Question

Remove all occurrences of an item in ArrayList

I am trying to remove all occurrences of an item in the arraylist

ArrayList list=new ArrayList();
list.Add("2.2");
list.Add("2.5");
list.Add("2.6");
list.Add("2.2");
list.Add("2.5");
list.Add("2.2");


How can remove all 2.2 values from the list?I have used

list.Remove("2.2")


but it removes only first occurrence

Answer Source

Read the docs on ArrayList.Remove(), in particular:

Removes the first occurrence of a specific object from the ArrayList.

Why are you using an ArrayList anyway? The elements in your example are all the same type, you would be better off using a List<string> and then using RemoveAll e.g.

List<string> list = new List<string>();
list.add("2.2");
list.add("2.5");
list.add("2.6");
list.add("2.2");
list.add("2.5");
list.add("2.2");

list.RemoveAll(item => item == "2.2");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download