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.
I don’t see why you couldn’t just simplify it to
You can see the MSDN Documentation for further details.
I don’t think you need to add any exception handling logic. The
Clearmethod internally usesArray.Clear, which as a reliability contract of Success and WillNotCorruptState. I couldn’t imagine how that would throw an exception.