Reputation: 43
I'm simply looking for a way to remove all values that == 0 in a List(int).
Thanks.
Upvotes: 2
Views: 8892
Reputation: 34782
Here are a few options:
Creating a new list based on the original list and filtering out the 0 values:
var newList = originalList.Where(i => i != 0).ToList();
Modifying the original list:
originalList.RemoveAll(i => i == 0);
The old-school, painful way (for funsies):
for (int i = 0; i < originalList.Length; i++)
{
if (originalList[i] == 0)
{
originalList.RemoveAt(i);
i--;
}
}
The really inefficient, traversing the list repeatedly way (don't do this):
while (originalList.Remove(0)) { }
Upvotes: 6