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.

Thanks

Recommended Answers

All 5 Replies

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).

as Momerath said:
ThisList.Clear();

All ICollections inherit the Clear method.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.