[Solved] Removing at most one occurrence of an item from a list [closed]


How about:

myList.RemoveAt(myList.indexOf(0));

And more generalized version would be:

void RemoveFirst<T>(List<T> list, T item)
{
    var indexOfItem = list.IndexOf(item);

    if(indexOfItem != -1)
        list.RemoveAt(indexOfItem);
}

Note: What .Remove() does shold be crystal clear to everyone. But when one wants to see the logic behind it, I think my answer still has some value.

14

solved Removing at most one occurrence of an item from a list [closed]