How to Quickly Remove Items From a List

前端 未结 11 1565
情歌与酒
情歌与酒 2020-11-30 01:45

I am looking for a way to quickly remove items from a C# List. The documentation states that the List.Remove() and List.RemoveAt()<

11条回答
  •  被撕碎了的回忆
    2020-11-30 02:14

    You could always remove the items from the end of the list. List removal is O(1) when performed on the last element since all it does is decrement count. There is no shifting of next elements involved. (which is the reason why list removal is O(n) generally)

    for (int i = list.Count - 1; i >= 0; --i)
      list.RemoveAt(i);
    

提交回复
热议问题