I'm simply looking for a way to remove all values that == 0 in a List(int).

Thanks.

有帮助吗?

解决方案

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)) { }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top