Remove list elements at given indices

后端 未结 7 915
无人共我
无人共我 2020-12-20 13:19

I have a list which contains some items of type string.

List lstOriginal;

I have another list which contains idices which sh

7条回答
  •  萌比男神i
    2020-12-20 13:35

    How are you populating the list of indices? There's a much more efficient RemoveAll method that you might be able to use. For example, instead of this:

    var indices = new List();
    int index = 0;
    foreach (var item in data)
        if (SomeFunction(data))
            indices.Add(index++);
    
    //then some logic to remove the items
    

    you could do this:

    data.RemoveAll(item => SomeFunction(item));
    

    This minimizes the copying of items to new positions in the array; each item is copied only once.

    You could also use a method group conversion in the above example, instead of a lambda:

    data.RemoveAll(SomeFunction);
    

提交回复
热议问题