I want to empty a list. How to do that?
To give an alternative answer (Who needs 5 equal answers?):
list.Add(5);
// list contains at least one element now
list = new List<int>();
// list in "list" is empty now
Keep in mind that all other references to the old list have not been cleared (depending on the situation, this might be what you want). Also, in terms of performance, it is usually a bit slower.
You need the Clear() function on the list, like so.
List<object> myList = new List<object>();
myList.Add(new object()); // Add something to the list
myList.Clear() // Our list is now empty
It's really easy:
myList.Clear();
You can use the clear method
List<string> test = new List<string>();
test.Clear();
If by "list" you mean a List<T>
, then the Clear method is what you want:
List<string> list = ...;
...
list.Clear();
You should get into the habit of searching the MSDN documentation on these things.
Here's how to quickly search for documentation on various bits of that type:
List<T>
class itself (this is where you should've started)All of these Google queries lists a bundle of links, but typically you want the first one that google gives you in each case.
you can do that
var list = new List<string>();
list.Clear();