How to Quickly Remove Items From a List

前端 未结 11 1542
情歌与酒
情歌与酒 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:00

    If you're happy creating a new list, you don't have to go through setting items to null. For example:

    // This overload of Where provides the index as well as the value. Unless
    // you need the index, use the simpler overload which just provides the value.
    List newList = oldList.Where((value, index) => index % 5 != 0)
                                  .ToList();
    

    However, you might want to look at alternative data structures, such as LinkedList or HashSet. It really depends on what features you need from your data structure.

提交回复
热议问题