How can I remove all the items of a list? I do not know any thing about how the items get indexed, thats why I cannot provide a better code except this..

for (int i = 0; i < _Cars.Count; i++)
            {

            }

Recommended Answers

All 5 Replies

_Cars.Clear()

Actually it's:

_Cars.Items.Clear();

;)

Actually, it's not List<T>.Clear(). List<T> doesn't have an items property.

oh f*** I saw listbox... my bad :p ur right ^^

Also, i realise you aren't going to use your code. But for future reference, if you are going to remove items from a collection in a for loop you should always start at the highest index and work back to zero. Otherwise, when you remove an item, the indexes all shift down one; the result is that you skip the next item and you risk an exception when you get to the last index because the collection is smaller than when you started.

You should use something like:

for(int i = _Cars.Count; i>=0; i--)
{
}
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.