Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all All Elements not working

I noticed this function in a .NET project I am working on.

    private static void RemoveAllElements(ref List<int> listToBeRemoved)
    {
        foreach (var i in listToBeRemoved)
        {
            listToBeRemoved.Remove(i);
        }
    }

Is this the quickest way to remove all elements from a list? I also noticed this function doesn't catch any exceptions. Should I change this? This is in existing code.

like image 574
abhi Avatar asked Nov 08 '11 15:11

abhi


People also ask

How to remove[] from a list in Python?

How to Remove an Element from a List Using the remove() Method in Python. To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.

How do I get rid of all childrens elements?

To remove all child nodes of an element, you can use the element's removeChild() method along with the lastChild property. The removeChild() method removes the given node from the specified element. It returns the removed node as a Node object, or null if the node is no longer available.

How do I select all elements on a page?

The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").


1 Answers

I don't see why you couldn't just simplify it to

listToBeRemoved.Clear();

You can see the MSDN Documentation for further details.

I don't think you need to add any exception handling logic. The Clear method internally uses Array.Clear, which as a reliability contract of Success and WillNotCorruptState. I couldn't imagine how that would throw an exception.

like image 79
vcsjones Avatar answered Sep 30 '22 03:09

vcsjones