I am having problems finding out how to remove everything from a List.

//Creating new List
List<Number> ThisList = new List<Number>();

Once it is populated and I have got it to display how do i remove all the contents within the list so i can use it to display different results.
I have had a look at RemoveAll, but i don't think that is what i want. If it is, I have no idea what condition I need to put in to remove all the elements from the List.


7 Years
Discussion Span
Last Post by Drakarus

You can use Clear() to empty a list, or you can just create a new one (and let the garbage collector clean up the old one).


Use the Clear() method to remove all element in the list.

RemoveAll is a delegate predicate that allows you to specify what conditions to remove the elements.

For example, lets say you had a list of integers and you wanted to remove only those that were a modulus of 2

List<int> myInts = new List<int>();
//add numbers

myInts.RemoveAll(delegate(SearchInt int)
  return SearchInt%2 == 0;

A true value removes it, a false value leaves it.

Edited by Venjense: n/a


Thank you all for the replies. Thanks for explaining how RemoveAll works as well.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.