拥有List<string> paths = new List<string>();
我想删除我不确定是否存在的项目。我应该检查它是否存在,还是直接运行 Remove 方法?
是if (paths.Exists(stringVar))
在 paths.Remove(stringVar)
之前需要或考虑好的做法?
如果列表中没有这样的项目,则在没有 Exists 的情况下运行 Remove 将简单地返回 false。
最佳答案
不,它不会抛出异常,也不需要额外的检查。见MSDN :
true if item is successfully removed; otherwise, false. This method also returns false if item was not found in the List.
关于c# - List<T>.Remove 应该放在 List<T>.Exists 之前吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6175048/