Pregunta

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

Thanks.

¿Fue útil?

Solución

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)) { }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top