Skip to content

Instantly share code, notes, and snippets.

@dend
Created January 17, 2013 06:25
Show Gist options
  • Save dend/4554128 to your computer and use it in GitHub Desktop.
Save dend/4554128 to your computer and use it in GitHub Desktop.
Proper implementation for RemoveAll<T>
public static void RemoveAll<T>(this List<T> list, Func<T, bool> filter)
{
if (filter == null)
throw new ArgumentException("filter");
if (list == null)
throw new ArgumentException("list");
int index = 0;
while ((index < list.Count) && !filter(list[index]))
{
index++;
}
if (index >= list.Count)
{
return;
}
int secondaryCounter = index + 1;
while (secondaryCounter < list.Count)
{
while ((secondaryCounter < list.Count) && filter(list[secondaryCounter]))
{
secondaryCounter++;
}
if (secondaryCounter < list.Count)
{
list[index++] = list[secondaryCounter++];
}
}
list.RemoveRange(index, list.Count - index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment