编辑:本问题底部发布的不同技术的基准。
我有一个非常大的List<int>整数。我想从 .txt 文件中删除所有出现的“3” List<int>。哪种技术最有效地做到这一点?我通常会使用.Remove(3)扩展直到它返回false,但我担心每次内部调用都会不必要地.Remove(3)循环整个过程List<int>。
编辑:评论中建议尝试
TheList = TheList.Where(x => x != 3).ToList();
但我需要删除元素而不实例化新的 List。
var TheList = new List<int> { 5, 7, 8, 2, 8, 3, 1, 0, 6, 3, 9, 3, 5, 2, 7, 9, 3, 5, 5, 1, 0, 4, 5, 3, 5, 8, 2, 3 };
//technique 1
//this technique has the shortest amount of code,
//but I fear that every time the …Run Code Online (Sandbox Code Playgroud)