Search code examples
c#removeall

Removing all elements from List<T>


I'm trying to remove all elements when the user clicks Yes on a messagebox, but I'm not really sure how to remove all items on a buttonclick. I am able to remove element at index but not all elements.

This is how I successfully remove element at index:

public void DeleteAt(int anIndex)
{
    if(CheckIndex(anIndex))
    m_list.RemoveAt(anIndex);
} 

But I want to remove all elements. I tried doing this:

public void DeleteAll()
{
      m_list.RemoveAll();
}

But it's not working, it's saying that there needs to be a parameter to RemoveAll(); but I don't know what kind of parameter.


Solution

  • List<T>.Clear() is what you want:

    Removes all elements from the List.

    So in your example:

    public void DeleteAll()
    {
        m_list.Clear(); 
    }